qdips.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <usb.h>
  5. #include <unistd.h>
  6. #include <getopt.h>
  7. #include "opendevice.h"
  8. #include "qdips.h"
  9. #define _16MBIT 0x200000
  10. #define HIROM_BANK_SIZE_SHIFT 16
  11. #define LOROM_BANK_SIZE_SHIFT 15
  12. #define HIROM_BANK_COUNT_SHIFT 32
  13. #define LOROM_BANK_COUNT_SHIFT 64
  14. #define IPS_MAGIC "PATCH"
  15. #define EOF_TAG 0x454f46
  16. #define PATCH_END 1
  17. #define PATCH_ERR 2
  18. #define PATCH_CHUNKS_MAX 512
  19. #define QD16_MAX_TX_LEN 128
  20. #define QD16_CMD_TIMEOUT 5000
  21. #define QD16_SEND_DATA(__addr_hi,__addr_lo,__src,__len)\
  22. usb_control_msg(handle,\
  23. USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_ENDPOINT_OUT,\
  24. USB_BULK_UPLOAD_NEXT,\
  25. __addr_hi, __addr_lo,\
  26. (char*)__src, __len,\
  27. QD16_CMD_TIMEOUT);
  28. #define QD16_SEND_ADDR(__addr_hi,__addr_lo,__src,__len)\
  29. usb_control_msg(handle,\
  30. USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_ENDPOINT_OUT,\
  31. USB_BULK_UPLOAD_ADDR,\
  32. __addr_hi,__addr_lo,\
  33. (char*)__src,__len,\
  34. QD16_CMD_TIMEOUT);
  35. typedef struct _ips_tag {
  36. uint32_t ofs; /* offset in file to patch */
  37. uint16_t len; /* number of bytes in this chunk */
  38. uint8_t* data; /* chunk data */
  39. } ips_tag_t;
  40. int qd16_apply_ips_chunk(ips_tag_t* ips_tag);
  41. int ips_read_next_tag(FILE* patch_file, ips_tag_t* tag);
  42. int qd16_init(void);
  43. void qd16_finish(void);
  44. void usage(void);
  45. usb_dev_handle *handle = NULL;
  46. const unsigned char rawVid[2] = { USB_CFG_VENDOR_ID }, rawPid[2] = { USB_CFG_DEVICE_ID};
  47. char vendor[] = { USB_CFG_VENDOR_NAME, 0 }, product[] = { USB_CFG_DEVICE_NAME, 0};
  48. int cnt, vid, pid;
  49. int hirom = 0;
  50. int header = 0;
  51. int init = 0;
  52. int quiet = 0;
  53. uint8_t dummy[128];
  54. void usage(void)
  55. {
  56. fprintf(stderr, "Usage: qdips [option] <patchfile>\n\n\
  57. Options:\n\
  58. -s\tSkip ROM header\n\
  59. -q\tBe quiet\n\
  60. -h\tForce HiROM mode\n\n");
  61. exit(1);
  62. }
  63. int main(int argc, char * argv[])
  64. {
  65. FILE* patch_file;
  66. char * fname = 0;
  67. int opt = 0;
  68. char magic[6];
  69. ips_tag_t ips_tags[PATCH_CHUNKS_MAX];
  70. int chunk_index = 0;
  71. if (argc==1)
  72. usage();
  73. while ((opt = getopt(argc,argv,"qish")) != -1){
  74. switch (opt) {
  75. case 'i':
  76. init = 1;
  77. break;
  78. case 's':
  79. header = 1;
  80. break;
  81. case 'h':
  82. hirom = 1;
  83. break;
  84. case 'q':
  85. quiet = 1;
  86. break;
  87. default:
  88. usage();
  89. }
  90. }
  91. if (optind >= argc) {
  92. usage();
  93. }
  94. int ret = qd16_init();
  95. if (ret != 0){
  96. fprintf(stderr,"Error during init. Exiting!\n");
  97. exit(1);
  98. }
  99. qd16_finish();
  100. exit(0);
  101. fname = argv[optind];
  102. patch_file = fopen(fname,"rb");
  103. if (patch_file == 0) {
  104. fprintf(stderr,"Failed to open file '%s'!\n",fname);
  105. exit(1);
  106. }
  107. if (fgets(magic,strlen(IPS_MAGIC)+1,patch_file) == NULL) {
  108. fprintf(stderr,"Error reading from file %s\n",fname);
  109. fclose(patch_file);
  110. exit(1);
  111. }
  112. if (strcmp(magic,IPS_MAGIC) != 0){
  113. fprintf(stderr,"Patch is not in ips format!\n");
  114. }
  115. int patch_error = 0;
  116. while (!feof(patch_file) && chunk_index < PATCH_CHUNKS_MAX) {
  117. printf(".");
  118. int ret = ips_read_next_tag(patch_file,&ips_tags[chunk_index]);
  119. if (ret == PATCH_END)
  120. break;
  121. else if (ret == PATCH_ERR) {
  122. patch_error = 1;
  123. break;
  124. }
  125. chunk_index++;
  126. }
  127. fclose(patch_file);
  128. for (int i=0; i<chunk_index; i++){
  129. printf("uploading chunk %d ..\n",i);
  130. int ret = qd16_apply_ips_chunk(&ips_tags[i]);
  131. if (ret!=0){
  132. printf("failed applying chunk %d !\n",i);
  133. }
  134. }
  135. qd16_finish();
  136. return 0;
  137. }
  138. int qd16_apply_ips_chunk(ips_tag_t* ips_tag)
  139. {
  140. return 0;
  141. int addr_hi = (ips_tag->ofs)>>16;
  142. int addr_lo = (ips_tag->ofs)&0xffff;
  143. printf("hi:%x lo:%x\n",addr_hi,addr_lo);
  144. int ret = QD16_SEND_ADDR(addr_hi,addr_lo,ips_tag->data,ips_tag->len);
  145. /*int ret = QD16_SEND_ADDR(addr_hi,addr_lo,dummy,128);*/
  146. if (ret<0){
  147. printf("error executing command!:%s\n",usb_strerror());
  148. }
  149. ret = QD16_SEND_DATA(addr_hi,addr_lo,ips_tag->data,ips_tag->len);
  150. /*int ret = QD16_SEND_ADDR(addr_hi,addr_lo,dummy,128);*/
  151. if (ret<0){
  152. printf("error executing command!:%s\n",usb_strerror());
  153. }
  154. return 0;
  155. }
  156. void qd16_finish(void)
  157. {
  158. #if 0
  159. cnt = usb_control_msg(handle,
  160. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  161. USB_ENDPOINT_OUT, USB_BULK_UPLOAD_END, 0, 0, NULL,
  162. 0, 5000);
  163. if (cnt<0) {
  164. printf("USB_BULK_UPLOAD_END failed: %s\n",usb_strerror());
  165. }
  166. #endif
  167. cnt = usb_control_msg(handle,
  168. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  169. USB_ENDPOINT_OUT, USB_SET_LOADER, 0, 0, NULL,
  170. 0, 5000);
  171. cnt = usb_control_msg(handle,
  172. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  173. USB_ENDPOINT_OUT, USB_MODE_SNES, 0, 0, NULL,
  174. 0, 5000);
  175. if (cnt<0) {
  176. printf("USB_MODE_SNES failed: %s\n",usb_strerror());
  177. }
  178. }
  179. int qd16_init(void)
  180. {
  181. usb_init();
  182. vid = rawVid[1] * 256 + rawVid[0];
  183. pid = rawPid[1] * 256 + rawPid[0];
  184. if (usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0) {
  185. fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid);
  186. return 1;
  187. }
  188. cnt = usb_control_msg(handle,
  189. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  190. USB_ENDPOINT_OUT, USB_MODE_AVR, 0, 0, NULL,
  191. 0, 5000);
  192. if (cnt<0) {
  193. printf("USB_MODE_AVR failed: %s\n",usb_strerror());
  194. }
  195. usleep(500000);
  196. #if 0
  197. cnt = usb_control_msg(handle,
  198. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
  199. USB_BULK_UPLOAD_INIT,
  200. (hirom ? HIROM_BANK_SIZE_SHIFT : LOROM_BANK_SIZE_SHIFT) ,
  201. (hirom ? HIROM_BANK_COUNT_SHIFT : LOROM_BANK_COUNT_SHIFT),
  202. NULL, 0, 5000);
  203. if (cnt<0) {
  204. printf("USB_BULK_UPLOAD_INIT failed: %s\n",usb_strerror());
  205. }
  206. #endif
  207. return 0;
  208. }
  209. int ips_read_next_tag(FILE* patch_file, ips_tag_t* tag)
  210. {
  211. tag->ofs = getc(patch_file)<<16 | getc(patch_file)<<8 | getc(patch_file);
  212. if (tag->ofs == EOF_TAG)
  213. return PATCH_END;
  214. if (tag->ofs > _16MBIT)
  215. return PATCH_ERR;
  216. tag->len = getc(patch_file)<<8 | getc(patch_file);
  217. tag->data = malloc(tag->len);
  218. size_t ret = fread(tag->data,1,tag->len,patch_file);
  219. if (ret != tag->len)
  220. return PATCH_ERR;
  221. return 0;
  222. }