Rockbox open source high quality audio player as a Music Player Daemon
mpris rockbox mpd libadwaita audio rust zig deno
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

sansapatcher: move sectorbuf pointer into sansa_t structure.

Similar as the ipod_t structure for ipodpatcher the sansa_t structure holds all
relevant information for sansapatcher. Put the global sansa_sectorbuf pointer
into it as well.

Change-Id: Iad08ef6aafc49609c3d0d556914246f230ee0179

+77 -97
+3 -4
rbutil/rbutilqt/base/autodetection.cpp
··· 167 167 168 168 // try sansapatcher 169 169 // initialize sector buffer. Needed. 170 - sansa_sectorbuf = NULL; 171 - sansa_alloc_buffer(&sansa_sectorbuf, BUFFER_SIZE); 172 170 struct sansa_t sansa; 171 + sansa_alloc_buffer(&sansa, BUFFER_SIZE); 173 172 n = sansa_scan(&sansa); 174 173 if(n == 1) { 175 174 qDebug() << "[Autodetect] Sansa found:" << sansa.targetname << "at" << sansa.diskname; ··· 187 186 else { 188 187 qDebug() << "[Autodetect] sansapatcher: no Sansa found." << n; 189 188 } 190 - free(sansa_sectorbuf); 191 - sansa_sectorbuf = NULL; 189 + free(sansa.sectorbuf); 190 + sansa.sectorbuf = NULL; 192 191 193 192 if(m_mountpoint.isEmpty() && m_device.isEmpty() 194 193 && m_errdev.isEmpty() && m_incompat.isEmpty())
+7 -18
rbutil/rbutilqt/base/bootloaderinstallsansa.cpp
··· 27 27 : BootloaderInstallBase(parent) 28 28 { 29 29 (void)parent; 30 - // initialize sector buffer. sansa_sectorbuf is instantiated by 31 - // sansapatcher. 32 - // The buffer itself is only present once, so make sure to not allocate 33 - // it if it was already allocated. The application needs to take care 34 - // no concurrent (i.e. multiple objects of this class running) requests 35 - // are done. 36 - if(sansa_sectorbuf == NULL) 37 - sansa_alloc_buffer(&sansa_sectorbuf, BUFFER_SIZE); 30 + // initialize sector buffer. The sector buffer is part of the sansa_t 31 + // structure, so a second instance of this class will have its own buffer. 32 + sansa_alloc_buffer(&sansa, BUFFER_SIZE); 38 33 } 39 34 40 35 41 36 BootloaderInstallSansa::~BootloaderInstallSansa() 42 37 { 43 - if(sansa_sectorbuf) { 44 - free(sansa_sectorbuf); 45 - sansa_sectorbuf = NULL; 38 + if(sansa.sectorbuf) { 39 + free(sansa.sectorbuf); 40 + sansa.sectorbuf = NULL; 46 41 } 47 42 } 48 43 ··· 51 46 */ 52 47 bool BootloaderInstallSansa::install(void) 53 48 { 54 - if(sansa_sectorbuf == NULL) { 49 + if(sansa.sectorbuf == NULL) { 55 50 emit logItem(tr("Error: can't allocate buffer memory!"), LOGERROR); 56 51 return false; 57 52 emit done(true); ··· 59 54 60 55 emit logItem(tr("Searching for Sansa"), LOGINFO); 61 56 62 - struct sansa_t sansa; 63 - 64 57 int n = sansa_scan(&sansa); 65 58 if(n == -1) { 66 59 emit logItem(tr("Permission for disc access denied!\n" ··· 95 88 */ 96 89 void BootloaderInstallSansa::installStage2(void) 97 90 { 98 - struct sansa_t sansa; 99 91 unsigned char* buf = NULL; 100 92 unsigned int len; 101 93 ··· 174 166 */ 175 167 bool BootloaderInstallSansa::uninstall(void) 176 168 { 177 - struct sansa_t sansa; 178 - 179 169 emit logItem(tr("Uninstalling bootloader"), LOGINFO); 180 170 QCoreApplication::processEvents(); 181 171 ··· 222 212 */ 223 213 BootloaderInstallBase::BootloaderType BootloaderInstallSansa::installed(void) 224 214 { 225 - struct sansa_t sansa; 226 215 int num; 227 216 228 217 if(!sansaInitialize(&sansa)) {
+2
rbutil/rbutilqt/base/bootloaderinstallsansa.h
··· 21 21 22 22 #include <QtCore> 23 23 #include "bootloaderinstallbase.h" 24 + #include "sansapatcher.h" 24 25 25 26 26 27 // bootloader installation class for devices handled by sansapatcher. ··· 38 39 39 40 private: 40 41 bool sansaInitialize(struct sansa_t *); 42 + struct sansa_t sansa; 41 43 42 44 private slots: 43 45 void installStage2(void);
+1 -1
rbutil/sansapatcher/main.c
··· 155 155 return 1; 156 156 } 157 157 158 - if (sansa_alloc_buffer(&sansa_sectorbuf,BUFFER_SIZE) < 0) { 158 + if (sansa_alloc_buffer(&sansa, BUFFER_SIZE) < 0) { 159 159 fprintf(stderr,"Failed to allocate memory buffer\n"); 160 160 } 161 161
+5 -5
rbutil/sansapatcher/sansaio-posix.c
··· 113 113 return 0; 114 114 } 115 115 116 - int sansa_alloc_buffer(unsigned char** sectorbuf, int bufsize) 116 + int sansa_alloc_buffer(struct sansa_t *sansa, int bufsize) 117 117 { 118 - *sectorbuf=malloc(bufsize); 119 - if (*sectorbuf == NULL) { 118 + sansa->sectorbuf=malloc(bufsize); 119 + if (sansa->sectorbuf == NULL) { 120 120 return -1; 121 121 } 122 122 return 0; ··· 139 139 return read(sansa->dh, buf, nbytes); 140 140 } 141 141 142 - int sansa_write(struct sansa_t* sansa, unsigned char* buf, int nbytes) 142 + int sansa_write(struct sansa_t* sansa, int nbytes) 143 143 { 144 - return write(sansa->dh, buf, nbytes); 144 + return write(sansa->dh, sansa->sectorbuf, nbytes); 145 145 } 146 146 #endif 147 147
+5 -5
rbutil/sansapatcher/sansaio-win32.c
··· 150 150 return 0; 151 151 } 152 152 153 - int sansa_alloc_buffer(unsigned char** sectorbuf, int bufsize) 153 + int sansa_alloc_buffer(struct sansa_t* sansa, int bufsize) 154 154 { 155 155 /* The ReadFile function requires a memory buffer aligned to a multiple of 156 156 the disk sector size. */ 157 - *sectorbuf = (unsigned char*)VirtualAlloc(NULL, bufsize, MEM_COMMIT, PAGE_READWRITE); 158 - if (*sectorbuf == NULL) { 157 + sansa->sectorbuf = (unsigned char*)VirtualAlloc(NULL, bufsize, MEM_COMMIT, PAGE_READWRITE); 158 + if (sansa->sectorbuf == NULL) { 159 159 sansa_print_error(" Error allocating a buffer: "); 160 160 return -1; 161 161 } ··· 189 189 return count; 190 190 } 191 191 192 - int sansa_write(struct sansa_t* sansa, unsigned char* buf, int nbytes) 192 + int sansa_write(struct sansa_t* sansa, int nbytes) 193 193 { 194 194 unsigned long count; 195 195 196 - if (!WriteFile(sansa->dh, buf, nbytes, &count, NULL)) { 196 + if (!WriteFile(sansa->dh, sansa->sectorbuf, nbytes, &count, NULL)) { 197 197 sansa_print_error(" Error writing to disk: "); 198 198 return -1; 199 199 }
+3 -2
rbutil/sansapatcher/sansaio.h
··· 63 63 64 64 struct sansa_t { 65 65 HANDLE dh; 66 + unsigned char* sectorbuf; 66 67 char diskname[4096]; 67 68 int sector_size; 68 69 struct sansa_partinfo_t pinfo[4]; ··· 77 78 int sansa_close(struct sansa_t* sansa); 78 79 int sansa_seek(struct sansa_t* sansa, loff_t pos); 79 80 int sansa_read(struct sansa_t* sansa, unsigned char* buf, int nbytes); 80 - int sansa_write(struct sansa_t* sansa, unsigned char* buf, int nbytes); 81 - int sansa_alloc_buffer(unsigned char** sectorbuf, int bufsize); 81 + int sansa_write(struct sansa_t* sansa, int nbytes); 82 + int sansa_alloc_buffer(struct sansa_t* sansa, int bufsize); 82 83 83 84 #ifdef __cplusplus 84 85 }
+51 -53
rbutil/sansapatcher/sansapatcher.c
··· 43 43 and initialise it with sansa_alloc_buf() in main(). 44 44 */ 45 45 46 - unsigned char* sansa_sectorbuf = NULL; 47 - 48 46 static off_t filesize(int fd) { 49 47 struct stat buf; 50 48 ··· 92 90 int i; 93 91 unsigned long count; 94 92 95 - count = sansa_read(sansa,sansa_sectorbuf, sansa->sector_size); 93 + count = sansa_read(sansa,sansa->sectorbuf, sansa->sector_size); 96 94 97 95 if (count <= 0) { 98 96 sansa_print_error(" Error reading from disk: "); 99 97 return -1; 100 98 } 101 99 102 - if ((sansa_sectorbuf[510] == 0x55) && (sansa_sectorbuf[511] == 0xaa)) { 100 + if ((sansa->sectorbuf[510] == 0x55) && (sansa->sectorbuf[511] == 0xaa)) { 103 101 /* parse partitions */ 104 102 for ( i = 0; i < 4; i++ ) { 105 - unsigned char* ptr = sansa_sectorbuf + 0x1be + 16*i; 103 + unsigned char* ptr = sansa->sectorbuf + 0x1be + 16*i; 106 104 sansa->pinfo[i].type = ptr[4]; 107 105 sansa->pinfo[i].start = BYTES2INT32(ptr, 8); 108 106 sansa->pinfo[i].size = BYTES2INT32(ptr, 12); ··· 112 110 /* not handled yet */ 113 111 } 114 112 } 115 - } else if ((sansa_sectorbuf[0] == 'E') && (sansa_sectorbuf[1] == 'R')) { 113 + } else if ((sansa->sectorbuf[0] == 'E') && (sansa->sectorbuf[1] == 'R')) { 116 114 if (!silent) fprintf(stderr,"[ERR] Bad boot sector signature\n"); 117 115 return -1; 118 116 } ··· 406 404 } 407 405 408 406 /* Check Bootloader header */ 409 - if (sansa_seek_and_read(sansa, sansa->start, sansa_sectorbuf, 0x200) < 0) { 407 + if (sansa_seek_and_read(sansa, sansa->start, sansa->sectorbuf, 0x200) < 0) { 410 408 return -2; 411 409 } 412 - if (memcmp(sansa_sectorbuf,"PPBL",4)!=0) { 410 + if (memcmp(sansa->sectorbuf,"PPBL",4)!=0) { 413 411 /* No bootloader header, abort */ 414 412 return -4; 415 413 } 416 - ppbl_length = (le2int(sansa_sectorbuf+4) + 0x1ff) & ~0x1ff; 414 + ppbl_length = (le2int(sansa->sectorbuf+4) + 0x1ff) & ~0x1ff; 417 415 418 416 /* Sanity/safety check - the bootloader can't be larger than PPMI_OFFSET */ 419 417 if (ppbl_length > PPMI_OFFSET) ··· 422 420 } 423 421 424 422 /* Load Sansa bootloader and check for "Sansa C200" magic string */ 425 - if (sansa_seek_and_read(sansa, sansa->start + 0x200, sansa_sectorbuf, ppbl_length) < 0) { 423 + if (sansa_seek_and_read(sansa, sansa->start + 0x200, sansa->sectorbuf, ppbl_length) < 0) { 426 424 fprintf(stderr,"[ERR] Seek and read to 0x%08llx in is_sansa failed.\n", 427 425 sansa->start+0x200); 428 426 return -6; 429 427 } 430 - if (sansa_memmem(sansa_sectorbuf, ppbl_length, "Sansa C200", 10) != NULL) { 428 + if (sansa_memmem(sansa->sectorbuf, ppbl_length, "Sansa C200", 10) != NULL) { 431 429 /* C200 */ 432 430 sansa->targetname="c200"; 433 431 } else { ··· 436 434 } 437 435 438 436 /* Check Main firmware header */ 439 - if (sansa_seek_and_read(sansa, sansa->start+PPMI_OFFSET, sansa_sectorbuf, 0x200) < 0) { 437 + if (sansa_seek_and_read(sansa, sansa->start+PPMI_OFFSET, sansa->sectorbuf, 0x200) < 0) { 440 438 fprintf(stderr,"[ERR] Seek to 0x%08llx in is_sansa failed.\n", 441 439 sansa->start+PPMI_OFFSET); 442 440 return -5; 443 441 } 444 - if (memcmp(sansa_sectorbuf,"PPMI",4)!=0) { 442 + if (memcmp(sansa->sectorbuf,"PPMI",4)!=0) { 445 443 /* No bootloader header, abort */ 446 444 return -7; 447 445 } 448 - ppmi_length = le2int(sansa_sectorbuf+4); 446 + ppmi_length = le2int(sansa->sectorbuf+4); 449 447 450 448 /* Check main mi4 file header */ 451 - if (sansa_seek_and_read(sansa, sansa->start+PPMI_OFFSET+0x200, sansa_sectorbuf, 0x200) < 0) { 449 + if (sansa_seek_and_read(sansa, sansa->start+PPMI_OFFSET+0x200, sansa->sectorbuf, 0x200) < 0) { 452 450 fprintf(stderr,"[ERR] Seek to 0x%08llx in is_sansa failed.\n", 453 451 sansa->start+PPMI_OFFSET+0x200); 454 452 return -5; 455 453 } 456 454 457 - if (get_mi4header(sansa_sectorbuf,&mi4header) < 0) { 455 + if (get_mi4header(sansa->sectorbuf,&mi4header) < 0) { 458 456 fprintf(stderr,"[ERR] Invalid mi4header\n"); 459 457 return -6; 460 458 } ··· 466 464 */ 467 465 468 466 sansa->hasoldbootloader = 0; 469 - if (memcmp(sansa_sectorbuf+0x1f8,"RBBL",4)==0) { 467 + if (memcmp(sansa->sectorbuf+0x1f8,"RBBL",4)==0) { 470 468 /* Look for an original firmware after the first image */ 471 469 if (sansa_seek_and_read(sansa, 472 470 sansa->start + PPMI_OFFSET + 0x200 + ppmi_length, 473 - sansa_sectorbuf, 512) < 0) { 471 + sansa->sectorbuf, 512) < 0) { 474 472 return -7; 475 473 } 476 474 477 - if (get_mi4header(sansa_sectorbuf,&mi4header)!=0) { 475 + if (get_mi4header(sansa->sectorbuf,&mi4header)!=0) { 478 476 fprintf(stderr,"[ERR] No original firmware found\n"); 479 477 sansa->hasoldbootloader = 1; 480 478 } ··· 659 657 int outfile; 660 658 struct mi4header_t mi4header; 661 659 662 - res = load_original_firmware(sansa,sansa_sectorbuf,&mi4header); 660 + res = load_original_firmware(sansa,sansa->sectorbuf,&mi4header); 663 661 if (res < 0) 664 662 return res; 665 663 ··· 669 667 return -1; 670 668 } 671 669 672 - res = write(outfile,sansa_sectorbuf,mi4header.mi4size); 670 + res = write(outfile,sansa->sectorbuf,mi4header.mi4size); 673 671 if (res != (int)mi4header.mi4size) { 674 672 fprintf(stderr,"[ERR] Write error - %d\n", res); 675 673 return -1; ··· 730 728 int n; 731 729 732 730 /* Create PPMI header */ 733 - memset(sansa_sectorbuf,0,0x200); 734 - memcpy(sansa_sectorbuf,"PPMI",4); 735 - int2le(bl_length, sansa_sectorbuf+4); 736 - int2le(0x00020000, sansa_sectorbuf+8); 731 + memset(sansa->sectorbuf,0,0x200); 732 + memcpy(sansa->sectorbuf,"PPMI",4); 733 + int2le(bl_length, sansa->sectorbuf+4); 734 + int2le(0x00020000, sansa->sectorbuf+8); 737 735 738 - /* copy bootloader to sansa_sectorbuf+0x200 */ 739 - memcpy(sansa_sectorbuf+0x200,bootloader,bl_length); 736 + /* copy bootloader to sansa->sectorbuf+0x200 */ 737 + memcpy(sansa->sectorbuf+0x200,bootloader,bl_length); 740 738 741 739 /* Load original firmware from Sansa to the space after the bootloader */ 742 - res = load_original_firmware(sansa,sansa_sectorbuf+0x200+bl_length,&mi4header); 740 + res = load_original_firmware(sansa,sansa->sectorbuf+0x200+bl_length,&mi4header); 743 741 if (res < 0) 744 742 return res; 745 743 ··· 753 751 754 752 length = 0x200 + bl_length + mi4header.mi4size; 755 753 756 - n=sansa_write(sansa, sansa_sectorbuf, length); 754 + n=sansa_write(sansa, length); 757 755 if (n < length) { 758 756 fprintf(stderr,"[ERR] Short write in add_bootloader\n"); 759 757 return -6; ··· 769 767 int n; 770 768 int length; 771 769 772 - /* Load original firmware from Sansa to sansa_sectorbuf+0x200 */ 773 - res = load_original_firmware(sansa,sansa_sectorbuf+0x200,&mi4header); 770 + /* Load original firmware from Sansa to sansa->sectorbuf+0x200 */ 771 + res = load_original_firmware(sansa,sansa->sectorbuf+0x200,&mi4header); 774 772 if (res < 0) 775 773 return res; 776 774 777 775 /* Create PPMI header */ 778 - memset(sansa_sectorbuf,0,0x200); 779 - memcpy(sansa_sectorbuf,"PPMI",4); 780 - int2le(mi4header.mi4size, sansa_sectorbuf+4); 781 - int2le(0x00020000, sansa_sectorbuf+8); 776 + memset(sansa->sectorbuf,0,0x200); 777 + memcpy(sansa->sectorbuf,"PPMI",4); 778 + int2le(mi4header.mi4size, sansa->sectorbuf+4); 779 + int2le(0x00020000, sansa->sectorbuf+8); 782 780 783 781 /* Now write the whole thing back to the Sansa */ 784 782 ··· 790 788 791 789 length = 0x200 + mi4header.mi4size; 792 790 793 - n=sansa_write(sansa, sansa_sectorbuf, length); 791 + n=sansa_write(sansa, length); 794 792 if (n < length) { 795 793 fprintf(stderr,"[ERR] Short write in delete_bootloader\n"); 796 794 return -6; ··· 808 806 int num = 0; 809 807 810 808 /* Check Main firmware header */ 811 - if (sansa_seek_and_read(sansa, sansa->start+PPMI_OFFSET, sansa_sectorbuf, 0x200) < 0) { 809 + if (sansa_seek_and_read(sansa, sansa->start+PPMI_OFFSET, sansa->sectorbuf, 0x200) < 0) { 812 810 return 0; 813 811 } 814 812 815 - ppmi_length = le2int(sansa_sectorbuf+4); 813 + ppmi_length = le2int(sansa->sectorbuf+4); 816 814 817 815 printf("[INFO] Image 1 - %llu bytes\n",ppmi_length); 818 816 num = 1; 819 817 820 818 /* Look for an original firmware after the first image */ 821 - if (sansa_seek_and_read(sansa, sansa->start + PPMI_OFFSET + 0x200 + ppmi_length, sansa_sectorbuf, 512) < 0) { 819 + if (sansa_seek_and_read(sansa, sansa->start + PPMI_OFFSET + 0x200 + ppmi_length, sansa->sectorbuf, 512) < 0) { 822 820 return 0; 823 821 } 824 822 825 - if (get_mi4header(sansa_sectorbuf,&mi4header)==0) { 823 + if (get_mi4header(sansa->sectorbuf,&mi4header)==0) { 826 824 printf("[INFO] Image 2 - %d bytes\n",mi4header.mi4size); 827 825 num = 2; 828 826 } ··· 874 872 of_length = filesize(infile); 875 873 876 874 /* Load original firmware from file */ 877 - memset(sansa_sectorbuf,0,0x200); 878 - n = read(infile,sansa_sectorbuf,of_length); 875 + memset(sansa->sectorbuf,0,0x200); 876 + n = read(infile,sansa->sectorbuf,of_length); 879 877 close(infile); 880 878 if (n < of_length) { 881 879 fprintf(stderr,"[ERR] Short read - requested %d bytes, received %d\n" ··· 884 882 } 885 883 886 884 /* Check we have a valid MI4 file. */ 887 - if (get_mi4header(sansa_sectorbuf,&mi4header)!=0) { 885 + if (get_mi4header(sansa->sectorbuf,&mi4header)!=0) { 888 886 fprintf(stderr,"[ERR] %s is not a valid mi4 file\n",filename); 889 887 return -1; 890 888 } 891 889 892 890 /* Decrypt and build the header */ 893 - if(prepare_original_firmware(sansa, sansa_sectorbuf, &mi4header)!=0){ 891 + if(prepare_original_firmware(sansa, sansa->sectorbuf, &mi4header)!=0){ 894 892 fprintf(stderr,"[ERR] Unable to build decrypted mi4 from %s\n" 895 893 ,filename); 896 894 return -1; ··· 903 901 return -1; 904 902 } 905 903 906 - n=sansa_write(sansa, sansa_sectorbuf, of_length); 904 + n=sansa_write(sansa, of_length); 907 905 if (n < of_length) { 908 906 fprintf(stderr,"[ERR] Short write in sansa_update_of\n"); 909 907 return -1; ··· 920 918 return -1; 921 919 } 922 920 923 - memset(sansa_sectorbuf,0,NVPARAMS_SIZE); 924 - n=sansa_write(sansa, sansa_sectorbuf, NVPARAMS_SIZE); 921 + memset(sansa->sectorbuf,0,NVPARAMS_SIZE); 922 + n=sansa_write(sansa, NVPARAMS_SIZE); 925 923 if (n < NVPARAMS_SIZE) { 926 924 fprintf(stderr,"[ERR] Short write in sansa_update_of\n"); 927 925 return -1; ··· 947 945 948 946 ppbl_length = filesize(infile); 949 947 950 - n = read(infile,sansa_sectorbuf+0x200,ppbl_length); 948 + n = read(infile,sansa->sectorbuf+0x200,ppbl_length); 951 949 close(infile); 952 950 if (n < ppbl_length) { 953 951 fprintf(stderr,"[ERR] Short read - requested %d bytes, received %d\n", ppbl_length, n); ··· 955 953 } 956 954 957 955 /* Step 2 - Build the header */ 958 - memset(sansa_sectorbuf,0,0x200); 959 - memcpy(sansa_sectorbuf,"PPBL",4); 960 - int2le(ppbl_length, sansa_sectorbuf+4); 961 - int2le(0x00010000, sansa_sectorbuf+8); 956 + memset(sansa->sectorbuf,0,0x200); 957 + memcpy(sansa->sectorbuf,"PPBL",4); 958 + int2le(ppbl_length, sansa->sectorbuf+4); 959 + int2le(0x00010000, sansa->sectorbuf+8); 962 960 963 961 /* Step 3 - write the bootloader to the Sansa */ 964 962 if (sansa_seek(sansa, sansa->start) < 0) { ··· 966 964 return -1; 967 965 } 968 966 969 - n=sansa_write(sansa, sansa_sectorbuf, ppbl_length + 0x200); 967 + n=sansa_write(sansa, ppbl_length + 0x200); 970 968 if (n < (ppbl_length+0x200)) { 971 969 fprintf(stderr,"[ERR] Short write in sansa_update_ppbl\n"); 972 970 return -1;
-9
rbutil/sansapatcher/sansapatcher.h
··· 32 32 /* Size of buffer for disk I/O - 8MB is large enough for any version 33 33 of the Apple firmware, but not the Nano's RSRC image. */ 34 34 #define BUFFER_SIZE 8*1024*1024 35 - #ifndef _MSC_VER 36 - extern unsigned char* sansa_sectorbuf; 37 - #else 38 - /* MSVC needs to use dllimport to allow using it directly from a DLL. 39 - * See http://support.microsoft.com/kb/90530 40 - * Building with MSVC is only when using as DLL. 41 - */ 42 - _declspec(dllimport) unsigned char* sansa_sectorbuf; 43 - #endif 44 35 45 36 int sansa_read_partinfo(struct sansa_t* sansa, int silent); 46 37 int is_sansa(struct sansa_t* sansa);