1
0

mktitanimg.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <libgen.h>
  5. #include "mktitanimg.h"
  6. struct checksumrecord
  7. {
  8. unsigned int magic;
  9. unsigned int chksum; /* The checksum for the complete header.
  10. Excepting the
  11. checksum block */
  12. };
  13. /***************************************************************************
  14. * void print_help(void)
  15. ***************************************************************************/
  16. void print_help(void)
  17. {
  18. static char* help_page[]=
  19. {
  20. "mknspimg version 1.0, Texas Instruments, 2004",
  21. "Syntax:",
  22. " mknspimg -o outfile -i image1 image2 -a align1 align2 [-v] [-b] [-p prod_id] [-r rel_id] [-s rel_name] [-f flags]",
  23. "Example:",
  24. " mknspimg -o nsp_image.bin -i kernel.bin files.img -a 0 4096",
  25. "This generates 'nsp_image.bin' from two input files aligning first to 0 and second to 4096 bytes."
  26. };
  27. int num_lines = sizeof(help_page)/sizeof(char*);
  28. int i;
  29. for(i=0; i < num_lines; i++) {
  30. printf("%s\n", help_page[i]);
  31. }
  32. }
  33. /***************************************************************************
  34. * void mknspimg_print_hdr(NSP_IMG_HDR* p_img_hdr)
  35. ***************************************************************************/
  36. void mknspimg_print_hdr(struct nsp_img_hdr *hdr)
  37. {
  38. struct nsp_img_hdr_chksum *chksum;
  39. struct nsp_img_hdr_section_info *sect_info;
  40. struct nsp_img_hdr_sections *section;
  41. int i;
  42. printf("****************** NSP Image Summary ******************\n");
  43. printf("Magic: 0x%x\n", hdr->head.magic);
  44. printf("Image Header Size: 0x%x bytes\n", hdr->head.hdr_size);
  45. printf("Total Image Size: %d bytes\n", hdr->head.image_size);
  46. printf("Product ID: 0x%x\n", hdr->head.prod_id);
  47. printf("Release ID: 0x%x\n", hdr->head.rel_id);
  48. printf("Version ID: 0x%x\n", hdr->head.version);
  49. printf("Offset Info: 0x%x\n", hdr->head.info_offset);
  50. printf("Offset Sect info: 0x%x\n", hdr->head.sect_info_offset);
  51. printf("Offset Sections: 0x%x\n", hdr->sect_info.sections_offset);
  52. chksum=(struct nsp_img_hdr_chksum *)(hdr+hdr->head.chksum_offset);
  53. printf("Header Checksum: 0x%x\n", chksum->hdr_chksum);
  54. printf("+++ Section Information +++\n");
  55. printf("# of sections: %u\n", hdr->sect_info.num_sects);
  56. section=&(hdr->sections);
  57. for(i = 0; i < hdr->sect_info.num_sects; i++, section++) {
  58. printf("+++++ Section %d +++++\n", i);
  59. printf("Total size: %u bytes\n", section->total_size);
  60. printf("Raw Size: %u bytes\n", section->raw_size);
  61. printf("Offset: 0x%x\n", section->offset);
  62. printf("Type: 0x%x\n", section->type);
  63. printf("Name: %s\n", section->name);
  64. }
  65. printf("*******************************************************\n");
  66. }
  67. CMDLINE_CFG cmd_line_cfg =
  68. {
  69. {
  70. /* MIN MAX FLAGS OPTION */
  71. { 2, 2, (CMDLINE_OPTFLAG_ALLOW | CMDLINE_OPTFLAG_MANDAT) }, /* '-a' align1 align2 */
  72. { 0, 0, CMDLINE_OPTFLAG_ALLOW }, /* '-b' bootstrap */
  73. { 0, 0, !CMDLINE_OPTFLAG_ALLOW }, /* '-c' */
  74. { 0, 0, !CMDLINE_OPTFLAG_ALLOW }, /* '-d' */
  75. { 0, 0, !CMDLINE_OPTFLAG_ALLOW }, /* '-e' */
  76. { 1, 1, CMDLINE_OPTFLAG_ALLOW }, /* '-f' flags */
  77. { 0, 0, !CMDLINE_OPTFLAG_ALLOW }, /* '-g' */
  78. { 1, 1, CMDLINE_OPTFLAG_ALLOW }, /* '-h' */
  79. { 2, 2, (CMDLINE_OPTFLAG_ALLOW | CMDLINE_OPTFLAG_MANDAT) }, /* '-i arg1 arg2 ' */
  80. { 0, 0, !CMDLINE_OPTFLAG_ALLOW }, /* '-j' */
  81. { 0, 0, !CMDLINE_OPTFLAG_ALLOW }, /* '-k' */
  82. { 0, 0, !CMDLINE_OPTFLAG_ALLOW }, /* '-l' */
  83. { 0, 0, !CMDLINE_OPTFLAG_ALLOW }, /* '-m' */
  84. { 0, 0, !CMDLINE_OPTFLAG_ALLOW }, /* '-n' */
  85. { 1, 1, (CMDLINE_OPTFLAG_ALLOW | CMDLINE_OPTFLAG_MANDAT) }, /* '-o arg' */
  86. { 1, 1, CMDLINE_OPTFLAG_ALLOW }, /* '-p' PROD_ID */
  87. { 0, 0, !CMDLINE_OPTFLAG_ALLOW }, /* '-q' */
  88. { 1, 1, CMDLINE_OPTFLAG_ALLOW }, /* '-r' REL_ID */
  89. { 1, 1, CMDLINE_OPTFLAG_ALLOW }, /* '-s' "Release XXX.XXX" */
  90. { 0, 0, !CMDLINE_OPTFLAG_ALLOW }, /* '-t' */
  91. { 0, 0, !CMDLINE_OPTFLAG_ALLOW }, /* '-u' */
  92. { 0, 0, CMDLINE_OPTFLAG_ALLOW }, /* '-v' control VERBOSE/NON-VERBOSE mode */
  93. { 0, 0, !CMDLINE_OPTFLAG_ALLOW }, /* '-w' */
  94. { 0, 0, !CMDLINE_OPTFLAG_ALLOW }, /* '-x' */
  95. { 0, 0, !CMDLINE_OPTFLAG_ALLOW }, /* '-y' */
  96. { 0, 0, !CMDLINE_OPTFLAG_ALLOW } /* '-z' */
  97. },
  98. { 0, 0, !CMDLINE_OPTFLAG_ALLOW }, /* global arguments */
  99. };
  100. /***************************************************************************
  101. * int nsp_img_write(void* image, char* file, int padding)
  102. * Write out the image.
  103. ***************************************************************************/
  104. int main(int argc, char* argv[], char* env[])
  105. {
  106. FILE* nsp_image = NULL;
  107. int header_version=1;
  108. int cmdline_err;
  109. char* cmdline_error_msg;
  110. char* filen_kernel;
  111. char* filen_files;
  112. char* filen_out;
  113. int i,count; /* loop variables */
  114. int num_sects = 2; /* We require exactly two image with -i option
  115. (see CMDLINE_CFG structure above) */
  116. int desc_count=0;
  117. int total = 0;
  118. int header_size=0;
  119. struct nsp_img_hdr_head *img_hdr_head; /* Start of image header */
  120. struct nsp_img_hdr_info *img_hdr_info;
  121. struct nsp_img_hdr_section_info *img_hdr_section_info ;
  122. struct nsp_img_hdr_sections *img_hdr_sections, *section; /* Section pointers */
  123. /* Configure the command line. */
  124. cmdline_configure(&cmd_line_cfg);
  125. /* Read and parse the command line. */
  126. cmdline_err = cmdline_read(argc, argv);
  127. /* Check for parsing errors. */
  128. if(cmdline_err != 0) {
  129. /* Get the parse error message */
  130. cmdline_error_msg = cmdline_error(cmdline_err);
  131. /* Print it out */
  132. printf("%s\n", cmdline_error_msg);
  133. /* Print our help too */
  134. print_help();
  135. return -1;
  136. }
  137. if(cmdline_getopt_count('h') > 0)
  138. {
  139. header_version=atoi(argv[cmdline_getarg(cmdline_getarg_list('h'),0)]);
  140. }
  141. /* Set up arguments */
  142. filen_kernel = argv[cmdline_getarg(cmdline_getarg_list('i'),0)];
  143. filen_files = argv[cmdline_getarg(cmdline_getarg_list('i'),1)];
  144. filen_out = argv[cmdline_getarg(cmdline_getarg_list('o'),0)];
  145. /* Command line arguments have been parsed. Start doing our work. */
  146. /* Caculate the header size, and allocate the memory, and assign the sub pointers */
  147. header_size = sizeof(struct nsp_img_hdr_head) + /* This has a single section
  148. desc block already */
  149. (header_version==1?0:4) +
  150. sizeof(struct nsp_img_hdr_info) +
  151. sizeof(struct nsp_img_hdr_section_info) +
  152. sizeof(struct nsp_img_hdr_sections) * num_sects ;
  153. img_hdr_head = (struct nsp_img_hdr_head *)malloc(header_size);
  154. memset(img_hdr_head, 0x0, header_size);
  155. img_hdr_info = (struct nsp_img_hdr_info*)((char *)img_hdr_head + sizeof(struct nsp_img_hdr_head) + (header_version==1?0:4));
  156. img_hdr_section_info = (struct nsp_img_hdr_section_info*)((char *)img_hdr_info + sizeof(struct nsp_img_hdr_info));
  157. img_hdr_sections = (struct nsp_img_hdr_sections*)((char *)img_hdr_section_info + sizeof(struct nsp_img_hdr_section_info));
  158. section = img_hdr_sections;
  159. memset(img_hdr_head, 0xff, (void*)img_hdr_info - (void*)img_hdr_head);
  160. img_hdr_head->hdr_version = header_version;
  161. img_hdr_head->hdr_size = header_size;
  162. img_hdr_head->info_offset = (void*)img_hdr_info - (void*)img_hdr_head;
  163. img_hdr_head->sect_info_offset = (void*)img_hdr_section_info - (void*)img_hdr_head;
  164. img_hdr_section_info->num_sects = num_sects;
  165. img_hdr_section_info->sect_size = sizeof(struct nsp_img_hdr_sections);
  166. img_hdr_section_info->sections_offset = (void*)img_hdr_sections - (void*)img_hdr_head;
  167. /* chksum = (struct nsp_img_hdr_chksum *)
  168. ((unsigned int)image_hdr + header_size - sizeof(struct nsp_img_hdr_chksum));*/
  169. /* Open the out file */
  170. nsp_image = fopen(filen_out,"wb+");
  171. if(nsp_image==NULL) {
  172. printf("ERROR: can't open %s for writing.\n", filen_out);
  173. return -1;
  174. }
  175. /* Skip image header. We'll come back to it after we've written out the images. */
  176. fseek(nsp_image,header_size,SEEK_SET);
  177. total = ftell(nsp_image);
  178. total = header_size;
  179. printf("total=%x\n",total);
  180. {
  181. int align;
  182. int padding;
  183. char * buf;
  184. align = (header_version==1?0x10000:0x4000);
  185. if(align==0) {
  186. /* The user indicated no padding */
  187. padding = 0;
  188. } else {
  189. /* Calculate number padding bytes */
  190. if((total %align) ==0)
  191. padding=0;
  192. else
  193. padding = align - (total % align);
  194. }
  195. if(padding>0)
  196. {
  197. buf=malloc(padding);
  198. memset(buf, 0xff, padding);
  199. if(fwrite((void*)buf,1,padding,nsp_image)!=padding) {
  200. printf("ERROR: can't write to %s.\n", filen_out);
  201. free(buf);
  202. return -1;
  203. }
  204. free(buf);
  205. }
  206. total+=padding;
  207. }
  208. /* Write out all specified images (with -i option) */
  209. for(i=0; i < num_sects; i++) {
  210. char* file_name; /* input file name */
  211. FILE* filep; /* input file pointer */
  212. int padding; /* number of padding bytes to prepend */
  213. int align; /* align factor from command line */
  214. int result; /* intermediate result */
  215. char * buf;
  216. /* Open the specified image for reading */
  217. file_name = argv[cmdline_getarg(cmdline_getarg_list('i'),i)];
  218. filep = fopen(file_name, "rb");
  219. if(filep==NULL) {
  220. printf("ERROR: can't open file %s for reading.\n", file_name);
  221. return -1;
  222. }
  223. section->flags = ~0x00;
  224. /* Determine file size */
  225. fseek(filep,0,SEEK_END);
  226. section->raw_size=ftell(filep);
  227. fseek(filep,0,SEEK_SET);
  228. cs_calc_sum(filep,(unsigned long *)&section->chksum,0);
  229. fseek(filep,0,SEEK_SET);
  230. /* Retrieve the alignment constant */
  231. /* Set image offset from the beginning of the out file */
  232. section->offset=total;// + padding;
  233. //total += padding;
  234. /* Copy the image file into nsp_image */
  235. count = section->raw_size;
  236. buf=malloc(count);
  237. result=fread(buf, 1, count, filep);
  238. fwrite(buf, 1, result, nsp_image);
  239. free(buf);
  240. /* HACK: This is a hack to get the names and types to the files.
  241. TODO: Fix this to be a real method */
  242. if(i==0){
  243. section->type=NSP_IMG_SECTION_TYPE_KERNEL;
  244. strncpy(section->name, "kernel", 16);
  245. } else if(i==1){
  246. section->type=NSP_IMG_SECTION_TYPE_FILESYSTEM_ROOT;
  247. strncpy(section->name, "root", 16);
  248. }
  249. /* Account for the total */
  250. align = strtoul(argv[cmdline_getarg(cmdline_getarg_list('a'),i)],NULL,0);
  251. if(i==0){
  252. if(align==0 || (((section->raw_size+ section->offset)%align)==0))
  253. padding=0;
  254. else
  255. padding = align - ((section->raw_size+ section->offset) % align);
  256. section->total_size=section->raw_size + padding;
  257. }
  258. else{
  259. #define EXTRA_BLOCK 0x10000
  260. unsigned int squash_padding;
  261. squash_padding = EXTRA_BLOCK - section->raw_size % EXTRA_BLOCK;
  262. buf=malloc(EXTRA_BLOCK + 4);
  263. memset(buf, 0, squash_padding);
  264. fwrite(buf, 1, squash_padding, nsp_image);
  265. memset(buf, 0, EXTRA_BLOCK + 4);
  266. *((unsigned int *)buf)=0xdec0adde;
  267. *((unsigned int *)(buf+EXTRA_BLOCK))=0xdec0adde;
  268. fwrite(buf, 1, EXTRA_BLOCK+4, nsp_image);
  269. free(buf);
  270. if(align==0 || (((section->raw_size + (EXTRA_BLOCK + 4 + squash_padding)) %align)==0))
  271. padding=0;
  272. else
  273. padding = align - ((section->raw_size + (EXTRA_BLOCK + 4 + squash_padding)) % align);
  274. section->total_size=section->raw_size + (EXTRA_BLOCK + 4 + squash_padding) + padding;
  275. }
  276. if(padding>0){
  277. buf=malloc(padding);
  278. memset(buf, 0xff, padding);
  279. fwrite(buf, 1, padding, nsp_image);
  280. free(buf);
  281. }
  282. printf("*****padding is %d\ttotal_size=%d\traw_size=%d\n",padding, section->total_size, section->raw_size);
  283. //total += section->raw_size;
  284. total = section->total_size + section->offset;
  285. printf("total=0x%x\n",total);
  286. /* Close the input file */
  287. fclose(filep);
  288. /* Move the section pointer to the next slot */
  289. section++;
  290. }
  291. /* Take care of the NSP image header fields */
  292. /* head fields */
  293. img_hdr_head->magic = NSP_IMG_MAGIC_NUMBER;
  294. img_hdr_head->boot_offset = img_hdr_sections->offset;
  295. img_hdr_head->flags = ~0x00; /* Set to all 1's */
  296. if(cmdline_getopt_count('b'))
  297. img_hdr_head->flags &= ~(NSP_IMG_FLAG_FAILBACK_5 | NSP_IMG_FLAG_FAILBACK_1);
  298. if(cmdline_getopt_count('f'))
  299. img_hdr_head->flags = strtoul(argv[cmdline_getarg(cmdline_getarg_list('f'),0)], 0, 16);
  300. #if 0
  301. img_hdr_head->hdr_version = 2;
  302. img_hdr_head->hdr_size = header_size;
  303. #endif
  304. if(cmdline_getopt_count('p'))
  305. img_hdr_head->prod_id = strtoul(argv[cmdline_getarg(cmdline_getarg_list('p'),0)], 0, 16);
  306. else
  307. img_hdr_head->prod_id = 0x4C575943;
  308. if(cmdline_getopt_count('r'))
  309. img_hdr_head->rel_id = strtoul(argv[cmdline_getarg(cmdline_getarg_list('r'),0)], 0, 0);
  310. else
  311. img_hdr_head->rel_id = 0x10203040;
  312. if(cmdline_getopt_count('s'))
  313. img_hdr_head->version = strtoul(argv[cmdline_getarg(cmdline_getarg_list('s'),0)], 0, 0);
  314. else
  315. img_hdr_head->version = 0x0b040000;
  316. img_hdr_head->image_size = total;
  317. #if 0
  318. img_hdr_head->info_offset = (unsigned int)(&(image_hdr->info)) -
  319. (unsigned int)image_hdr;
  320. img_hdr_head->sect_info_offset= (unsigned int)(&(image_hdr->sect_info)) -
  321. (unsigned int)image_hdr;
  322. #endif
  323. // image_hdr->head.chksum_offset = (unsigned int)chksum - (unsigned int)image_hdr;
  324. img_hdr_head->chksum_offset = 0xffffffff;
  325. // image_hdr->head.pad1 = 0xffffffff;
  326. /* info fields */
  327. /* TODO: Fix. Do nothing yet */
  328. // strncpy(nsp_img_hdr.id.prod_info,NSP_PRODINFO_STRING,sizeof(NSP_PRODINFO_STRING));
  329. strcpy(img_hdr_info->image_filename, (const char *)basename(filen_out));
  330. /* section fields */
  331. #if 0
  332. img_hdr_section_info->num_sects= num_sects;
  333. img_hdr_section_info->sect_size= sizeof(struct nsp_img_hdr_sections);
  334. img_hdr_section_info->sections_offset= (unsigned int)(&(image_hdr->sections)) -
  335. (unsigned int)image_hdr;
  336. #endif
  337. /* Calculate checksum(s) */
  338. #if 0
  339. chksum->hdr_chksum = cs_calc_buf_sum((char*)image_hdr,
  340. header_size - sizeof(struct nsp_img_hdr_chksum));
  341. #endif
  342. /* Write out the NSP header. */
  343. fseek(nsp_image,0,SEEK_SET);
  344. count = fwrite((void*)img_hdr_head, header_size, 1, nsp_image);
  345. if(count!=1) {
  346. printf("ERROR: can't write to %s.\n", filen_out);
  347. return -1;
  348. }
  349. /* Check if -v option was specified (no arg needed) */
  350. if(cmdline_getopt_count('v') > 0)
  351. {
  352. struct nsp_img_hdr_head head;
  353. struct nsp_img_hdr *hdr;
  354. /* Rewind the file back to the beginning */
  355. fseek(nsp_image,0,SEEK_SET);
  356. /* Read header from the file */
  357. fread((void*)&head, sizeof(struct nsp_img_hdr_head),
  358. 1, nsp_image);
  359. /* Get memory to store the complete header */
  360. hdr = (struct nsp_img_hdr *)malloc(head.hdr_size);
  361. /* Read header from the file */
  362. fseek(nsp_image,0,SEEK_SET);
  363. fread((void*)hdr, head.hdr_size, 1, nsp_image);
  364. /* Print it out */
  365. mknspimg_print_hdr(hdr);
  366. printf("Generated total %d bytes\n",total);
  367. free(hdr);
  368. }
  369. free(img_hdr_head);
  370. {
  371. struct checksumrecord cr;
  372. cr.magic=CKSUM_MAGIC_NUMBER;
  373. cs_calc_sum(nsp_image, (unsigned long *)&cr.chksum, 0);
  374. fseek(nsp_image,0, SEEK_END);
  375. fwrite(&cr, 1, sizeof(cr), nsp_image);
  376. }
  377. {
  378. FILE * non_web;
  379. char fname[256];
  380. char * img_buf;
  381. unsigned int len;
  382. strcpy(fname, filen_out);
  383. strcat(fname, ".non_web");
  384. non_web = fopen(fname,"wb+");
  385. fseek(nsp_image, 0, SEEK_END);
  386. len = ftell(nsp_image);
  387. img_buf=malloc(len);
  388. fseek(nsp_image, 0, SEEK_SET);
  389. fread(img_buf, 1, len, nsp_image);
  390. img_buf[0xb] = 0x17;
  391. fwrite(img_buf, 1, len-sizeof(struct checksumrecord), non_web);
  392. fclose(non_web);
  393. free(img_buf);
  394. }
  395. /* Close NSP image file */
  396. fclose(nsp_image);
  397. /* return result */
  398. return(0);
  399. }
  400. #ifdef DMALLOC
  401. #include <dmalloc.h>
  402. #endif /* DMALLOC */
  403. #define BUFLEN (1 << 16)
  404. static unsigned long crctab[256] =
  405. {
  406. 0x0,
  407. 0x04C11DB7, 0x09823B6E, 0x0D4326D9, 0x130476DC, 0x17C56B6B,
  408. 0x1A864DB2, 0x1E475005, 0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6,
  409. 0x2B4BCB61, 0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD,
  410. 0x4C11DB70, 0x48D0C6C7, 0x4593E01E, 0x4152FDA9, 0x5F15ADAC,
  411. 0x5BD4B01B, 0x569796C2, 0x52568B75, 0x6A1936C8, 0x6ED82B7F,
  412. 0x639B0DA6, 0x675A1011, 0x791D4014, 0x7DDC5DA3, 0x709F7B7A,
  413. 0x745E66CD, 0x9823B6E0, 0x9CE2AB57, 0x91A18D8E, 0x95609039,
  414. 0x8B27C03C, 0x8FE6DD8B, 0x82A5FB52, 0x8664E6E5, 0xBE2B5B58,
  415. 0xBAEA46EF, 0xB7A96036, 0xB3687D81, 0xAD2F2D84, 0xA9EE3033,
  416. 0xA4AD16EA, 0xA06C0B5D, 0xD4326D90, 0xD0F37027, 0xDDB056FE,
  417. 0xD9714B49, 0xC7361B4C, 0xC3F706FB, 0xCEB42022, 0xCA753D95,
  418. 0xF23A8028, 0xF6FB9D9F, 0xFBB8BB46, 0xFF79A6F1, 0xE13EF6F4,
  419. 0xE5FFEB43, 0xE8BCCD9A, 0xEC7DD02D, 0x34867077, 0x30476DC0,
  420. 0x3D044B19, 0x39C556AE, 0x278206AB, 0x23431B1C, 0x2E003DC5,
  421. 0x2AC12072, 0x128E9DCF, 0x164F8078, 0x1B0CA6A1, 0x1FCDBB16,
  422. 0x018AEB13, 0x054BF6A4, 0x0808D07D, 0x0CC9CDCA, 0x7897AB07,
  423. 0x7C56B6B0, 0x71159069, 0x75D48DDE, 0x6B93DDDB, 0x6F52C06C,
  424. 0x6211E6B5, 0x66D0FB02, 0x5E9F46BF, 0x5A5E5B08, 0x571D7DD1,
  425. 0x53DC6066, 0x4D9B3063, 0x495A2DD4, 0x44190B0D, 0x40D816BA,
  426. 0xACA5C697, 0xA864DB20, 0xA527FDF9, 0xA1E6E04E, 0xBFA1B04B,
  427. 0xBB60ADFC, 0xB6238B25, 0xB2E29692, 0x8AAD2B2F, 0x8E6C3698,
  428. 0x832F1041, 0x87EE0DF6, 0x99A95DF3, 0x9D684044, 0x902B669D,
  429. 0x94EA7B2A, 0xE0B41DE7, 0xE4750050, 0xE9362689, 0xEDF73B3E,
  430. 0xF3B06B3B, 0xF771768C, 0xFA325055, 0xFEF34DE2, 0xC6BCF05F,
  431. 0xC27DEDE8, 0xCF3ECB31, 0xCBFFD686, 0xD5B88683, 0xD1799B34,
  432. 0xDC3ABDED, 0xD8FBA05A, 0x690CE0EE, 0x6DCDFD59, 0x608EDB80,
  433. 0x644FC637, 0x7A089632, 0x7EC98B85, 0x738AAD5C, 0x774BB0EB,
  434. 0x4F040D56, 0x4BC510E1, 0x46863638, 0x42472B8F, 0x5C007B8A,
  435. 0x58C1663D, 0x558240E4, 0x51435D53, 0x251D3B9E, 0x21DC2629,
  436. 0x2C9F00F0, 0x285E1D47, 0x36194D42, 0x32D850F5, 0x3F9B762C,
  437. 0x3B5A6B9B, 0x0315D626, 0x07D4CB91, 0x0A97ED48, 0x0E56F0FF,
  438. 0x1011A0FA, 0x14D0BD4D, 0x19939B94, 0x1D528623, 0xF12F560E,
  439. 0xF5EE4BB9, 0xF8AD6D60, 0xFC6C70D7, 0xE22B20D2, 0xE6EA3D65,
  440. 0xEBA91BBC, 0xEF68060B, 0xD727BBB6, 0xD3E6A601, 0xDEA580D8,
  441. 0xDA649D6F, 0xC423CD6A, 0xC0E2D0DD, 0xCDA1F604, 0xC960EBB3,
  442. 0xBD3E8D7E, 0xB9FF90C9, 0xB4BCB610, 0xB07DABA7, 0xAE3AFBA2,
  443. 0xAAFBE615, 0xA7B8C0CC, 0xA379DD7B, 0x9B3660C6, 0x9FF77D71,
  444. 0x92B45BA8, 0x9675461F, 0x8832161A, 0x8CF30BAD, 0x81B02D74,
  445. 0x857130C3, 0x5D8A9099, 0x594B8D2E, 0x5408ABF7, 0x50C9B640,
  446. 0x4E8EE645, 0x4A4FFBF2, 0x470CDD2B, 0x43CDC09C, 0x7B827D21,
  447. 0x7F436096, 0x7200464F, 0x76C15BF8, 0x68860BFD, 0x6C47164A,
  448. 0x61043093, 0x65C52D24, 0x119B4BE9, 0x155A565E, 0x18197087,
  449. 0x1CD86D30, 0x029F3D35, 0x065E2082, 0x0B1D065B, 0x0FDC1BEC,
  450. 0x3793A651, 0x3352BBE6, 0x3E119D3F, 0x3AD08088, 0x2497D08D,
  451. 0x2056CD3A, 0x2D15EBE3, 0x29D4F654, 0xC5A92679, 0xC1683BCE,
  452. 0xCC2B1D17, 0xC8EA00A0, 0xD6AD50A5, 0xD26C4D12, 0xDF2F6BCB,
  453. 0xDBEE767C, 0xE3A1CBC1, 0xE760D676, 0xEA23F0AF, 0xEEE2ED18,
  454. 0xF0A5BD1D, 0xF464A0AA, 0xF9278673, 0xFDE69BC4, 0x89B8FD09,
  455. 0x8D79E0BE, 0x803AC667, 0x84FBDBD0, 0x9ABC8BD5, 0x9E7D9662,
  456. 0x933EB0BB, 0x97FFAD0C, 0xAFB010B1, 0xAB710D06, 0xA6322BDF,
  457. 0xA2F33668, 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4
  458. };
  459. int cs_is_tagged(FILE *fp)
  460. {
  461. char buf[8];
  462. fseek(fp, -8, SEEK_END);
  463. fread(buf, 8, 1, fp);
  464. if(*(unsigned long*)buf == CKSUM_MAGIC_NUMBER)
  465. return 1;
  466. return 0;
  467. }
  468. unsigned long cs_read_sum(FILE *fp)
  469. {
  470. char buf[8];
  471. fseek(fp, -8, SEEK_END);
  472. fread(buf, 8, 1, fp);
  473. return *((unsigned long*)&buf[4]);
  474. }
  475. int cs_calc_sum(FILE *fp, unsigned long *res, int tagged)
  476. {
  477. unsigned char buf[BUFLEN];
  478. unsigned long crc = 0;
  479. uintmax_t length = 0;
  480. size_t bytes_read;
  481. fseek(fp, 0, SEEK_SET);
  482. while((bytes_read = fread(buf, 1, BUFLEN, fp)) > 0)
  483. {
  484. unsigned char *cp = buf;
  485. if(length + bytes_read < length)
  486. return 0;
  487. if(bytes_read != BUFLEN && tagged)
  488. bytes_read -= 8;
  489. length += bytes_read;
  490. while(bytes_read--)
  491. crc =(crc << 8) ^ crctab[((crc >> 24) ^ *cp++) & 0xFF];
  492. }
  493. if(ferror(fp))
  494. return 0;
  495. for(; length; length >>= 8)
  496. crc =(crc << 8) ^ crctab[((crc >> 24) ^ length) & 0xFF];
  497. crc = ~crc & 0xFFFFFFFF;
  498. *res = crc;
  499. return 1;
  500. }
  501. unsigned long cs_calc_buf_sum(char *buf, int size)
  502. {
  503. unsigned long crc = 0;
  504. char *cp = buf;
  505. unsigned long length = size;
  506. while(size--)
  507. crc =(crc << 8) ^ crctab[((crc >> 24) ^ *cp++) & 0xFF];
  508. for(; length; length >>= 8)
  509. crc =(crc << 8) ^ crctab[((crc >> 24) ^ length) & 0xFF];
  510. crc = ~crc & 0xFFFFFFFF;
  511. return crc;
  512. }
  513. unsigned long cs_calc_buf_sum_ds(char *buf, int buf_size, char *sign, int sign_len)
  514. {
  515. unsigned long crc = 0;
  516. char *cp = buf;
  517. unsigned long length = buf_size+sign_len;
  518. while(buf_size--)
  519. crc =(crc << 8) ^ crctab[((crc >> 24) ^ *cp++) & 0xFF];
  520. cp = sign;
  521. while(sign_len--)
  522. crc =(crc << 8) ^ crctab[((crc >> 24) ^ *cp++) & 0xFF];
  523. for(; length; length >>= 8)
  524. crc =(crc << 8) ^ crctab[((crc >> 24) ^ length) & 0xFF];
  525. crc = ~crc & 0xFFFFFFFF;
  526. return crc;
  527. }
  528. int cs_set_sum(FILE *fp, unsigned long sum, int tagged)
  529. {
  530. unsigned long magic = CKSUM_MAGIC_NUMBER;
  531. if(tagged)
  532. fseek(fp, -8, SEEK_END);
  533. else
  534. fseek(fp, 0, SEEK_END);
  535. if(fwrite(&magic, 1, 4, fp) < 4)
  536. return 0;
  537. if(fwrite(&sum, 1, 4, fp) < 4)
  538. return 0;
  539. return 1;
  540. }
  541. void cs_get_sum(FILE *fp, unsigned long *sum)
  542. {
  543. unsigned long magic = 0;
  544. fseek(fp, -8, SEEK_END);
  545. fread(&magic, 4, 1, fp);
  546. fread(sum, 4, 1, fp);
  547. }
  548. int cs_validate_file(char *filename)
  549. {
  550. FILE *pFile = NULL;
  551. unsigned long sum = 0, res = 0;
  552. if((pFile = fopen(filename, "r")) == NULL)
  553. return 0;
  554. if(!cs_is_tagged(pFile))
  555. {
  556. fclose(pFile);
  557. return 0;
  558. }
  559. if(!cs_calc_sum(pFile, &sum, 1))
  560. {
  561. fclose(pFile);
  562. return 0;
  563. }
  564. cs_get_sum(pFile, &res);
  565. fclose(pFile);
  566. if(sum != res)
  567. return 0;
  568. return 1;
  569. }
  570. /* ********* Library internal data ********* */
  571. #define CMDLINE_TRUE 1
  572. #define CMDLINE_FALSE 0
  573. typedef enum CMDLINE_ERR
  574. {
  575. CMDLINE_ERR_OK = 0, /* No Error (OK) */
  576. CMDLINE_ERR_ERROR = -1, /* Unspecified error */
  577. CMDLINE_ERR_INVKEY = -3, /* Invalid option key */
  578. CMDLINE_ERR_MANYARG = -4, /* Too many arguments */
  579. CMDLINE_ERR_FEWARG = -5, /* Too few arguments */
  580. CMDLINE_ERR_ILLOPT = -6, /* Option not allowed (illegal option) */
  581. CMDLINE_ERR_NOMEM = -7, /* No memory */
  582. CMDLINE_ERR_OPTMIS = -8 /* A mandatory option is missing */
  583. } CMDLINE_ERR;
  584. /* Argument list */
  585. typedef struct CMDLINE_ARG
  586. {
  587. int index; /* Index of the argument in the command line */
  588. struct CMDLINE_ARG* p_next; /* Next node in the linked list */
  589. } CMDLINE_ARG;
  590. /* Master control block for an option */
  591. typedef struct CMDLINE_ARGS
  592. {
  593. int argc; /* Total count of arguments found */
  594. int optc; /* Total count of options found */
  595. CMDLINE_ARG* list; /* Argument list */
  596. } CMDLINE_ARGS;
  597. /* Master control block for all found arguments */
  598. typedef struct CMDLINE_DATA
  599. {
  600. CMDLINE_ARGS opt_args[26]; /* Array of MCBs for each option ('a' through 'z') */
  601. CMDLINE_ARGS glb_args; /* Global arguments */
  602. int parsed; /* Internal flag to prevent client calls if library is not initialized */
  603. } CMDLINE_DATA;
  604. /* ********* Local Data ********* */
  605. static CMDLINE_CFG cmdline_cfg;
  606. static CMDLINE_DATA cmdline_data;
  607. char* cmdline_errmsg = "CMDLINE ERROR";
  608. /* ***************************************************************
  609. * Print all found command line options and their arguments
  610. ****************************************************************** */
  611. void* cmdline_getarg_list(char opt)
  612. {
  613. int index = (opt - 'a');
  614. /* Check the validity of the index */
  615. if((index < 0) || (index > 25))
  616. {
  617. /* ERROR: Wrong option */
  618. return NULL;
  619. }
  620. /* Return a pointer to the ARGS control structure */
  621. return((void*)(&cmdline_data.opt_args[index]));
  622. }
  623. /* ***************************************************************
  624. * Print all found command line options and their arguments
  625. ****************************************************************** */
  626. int cmdline_getarg_count(void* list)
  627. {
  628. CMDLINE_ARGS* p_args = (CMDLINE_ARGS*)list;
  629. /* Return number of arguments for this option */
  630. return(p_args->argc);
  631. }
  632. /* ***************************************************************
  633. * Print all found command line options and their arguments
  634. ****************************************************************** */
  635. int cmdline_getopt_count(char opt)
  636. {
  637. int index;
  638. /* Calculate index value */
  639. index = opt - 'a';
  640. if(index < 0 || index > 25) return -1;
  641. /* Return number of arguments for this option */
  642. return(cmdline_data.opt_args[index].optc);
  643. }
  644. /* ***************************************************************
  645. * Print all found command line options and their arguments
  646. ****************************************************************** */
  647. int cmdline_getarg(void* list, int num)
  648. {
  649. int i;
  650. CMDLINE_ARGS* p_args = (CMDLINE_ARGS*)list;
  651. CMDLINE_ARG* p_arg;
  652. /* Search the 'num' argument in the list for this option */
  653. for(i=0,p_arg=p_args->list; (p_arg!=NULL) && (i<p_args->argc); i++, p_arg=p_arg->p_next)
  654. {
  655. /* if num matches i, we found it */
  656. if(i==num) return(p_arg->index);
  657. }
  658. /* We did not find the specified argument or the list was empty */
  659. return -1;
  660. }
  661. /* ***************************************************************
  662. * Print all found command line options and their arguments
  663. ****************************************************************** */
  664. int cmdline_configure(CMDLINE_CFG* p_cfg)
  665. {
  666. /* reset global data */
  667. memset(&cmdline_cfg,0,sizeof(cmdline_cfg));
  668. memset(&cmdline_data,0,sizeof(cmdline_data));
  669. /* Copy the user's config structure */
  670. cmdline_cfg = *p_cfg;
  671. return 0;
  672. }
  673. /* ***************************************************************
  674. * Print all found command line options and their arguments
  675. ****************************************************************** */
  676. char* cmdline_error(int err)
  677. {
  678. /* TODO: implement a table of error messages */
  679. return(cmdline_errmsg);
  680. }
  681. /* ***************************************************************
  682. * Print all found command line options and their arguments
  683. ****************************************************************** */
  684. static void cmdline_print_args(CMDLINE_ARGS* p_arglist, char* argv[])
  685. {
  686. CMDLINE_ARG* p_arg;
  687. printf(" Number of times option was specified: %d\n", p_arglist->optc);
  688. printf(" Number of Arguments: %d\n", p_arglist->argc);
  689. if(p_arglist->argc > 0)
  690. {
  691. printf(" Argument List: ");
  692. for(p_arg=p_arglist->list; p_arg != NULL; p_arg=p_arg->p_next)
  693. printf("%s ", argv[p_arg->index]);
  694. }
  695. printf("\n");
  696. }
  697. /* ***************************************************************
  698. * Print all found command line options and their arguments
  699. ****************************************************************** */
  700. void cmdline_print(char* argv[])
  701. {
  702. int i;
  703. /* Check if the command line was parsed */
  704. if(cmdline_data.parsed != CMDLINE_TRUE)
  705. {
  706. printf("The command line has not been parsed yet.\n");
  707. return;
  708. }
  709. /* Print out option arguments */
  710. for( i = 0; i < 26; i++ )
  711. {
  712. /* Check if the option was specified */
  713. if(cmdline_data.opt_args[i].optc !=0 )
  714. {
  715. /* Print out option name and arguments */
  716. printf("Option: -%c\n", (char)('a'+i));
  717. cmdline_print_args(&(cmdline_data.opt_args[i]), argv);
  718. }
  719. }
  720. /* Print out global arguments */
  721. printf("Global arguments:\n");
  722. cmdline_print_args(&(cmdline_data.glb_args), argv);
  723. }
  724. /* ***************************************************************
  725. * Print configuration
  726. ****************************************************************** */
  727. void cmdline_print_cfg(void)
  728. {
  729. }
  730. static void cmdline_argadd(CMDLINE_ARGS* p_arglist, CMDLINE_ARG* p_arg)
  731. {
  732. CMDLINE_ARG* p_list;
  733. CMDLINE_ARG* p_prev=NULL;
  734. /* See if we had anything in the list */
  735. if(p_arglist->argc == 0)
  736. {
  737. /* Link the argument in */
  738. p_arglist->list = p_arg;
  739. }
  740. else
  741. {
  742. /* Find the tail of the list */
  743. for(p_list=p_arglist->list; p_list != NULL; p_list=p_list->p_next)
  744. p_prev = p_list;
  745. /* Link the argument in */
  746. p_prev->p_next=p_arg;
  747. }
  748. /* Keep track of arg number */
  749. p_arglist->argc++;
  750. }
  751. /* ***************************************************************
  752. * cmdline_read()
  753. * Read and parse command line arguments
  754. ****************************************************************** */
  755. int cmdline_read(int argc, char* argv[])
  756. {
  757. int i, option=0;
  758. /* Process every command line argument in argv[] array */
  759. for( i = 1; i < argc; i++ )
  760. {
  761. /* Does the argument start with a dash? */
  762. if( *argv[i] == '-' )
  763. {
  764. /* The argument must be two characters: a dash, and a letter */
  765. if( strlen(argv[i]) != 2 )
  766. {
  767. /* ERROR: option syntax (needs to be a dash and one letter) */
  768. return(CMDLINE_ERR_ERROR);
  769. }
  770. /* Check validity of the option key ('a' through 'z') */
  771. if( ((*(argv[i] + 1)) < 'a') || ((*(argv[i] + 1)) > 'z') )
  772. {
  773. /* ERROR: option sysntax (invalid option key) */
  774. return(CMDLINE_ERR_INVKEY);
  775. }
  776. /* Calculate the option index */
  777. option = (*(argv[i] + 1)) - 'a';
  778. if((option < 0) || (option > 25)) return(CMDLINE_ERR_INVKEY);
  779. /* Check to see if the option is allowed */
  780. if( cmdline_cfg.opts[option].flags & CMDLINE_OPTFLAG_ALLOW )
  781. {
  782. /* Option allowed. */
  783. cmdline_data.opt_args[option].optc++;
  784. continue;
  785. }
  786. else
  787. {
  788. /* ERROR: Option is not allowed */
  789. return(CMDLINE_ERR_ILLOPT);
  790. }
  791. }
  792. else
  793. {
  794. /* Read the arguments for the option */
  795. CMDLINE_ARG* p_arg;
  796. /* Allocate space for the argument node */
  797. p_arg = (CMDLINE_ARG*)calloc(1,sizeof(CMDLINE_ARG));
  798. if( p_arg== NULL )
  799. {
  800. /* ERROR: Can't allocate memory for the argument index */
  801. return(CMDLINE_ERR_NOMEM);
  802. }
  803. /* Initialize the argument */
  804. p_arg->index = i;
  805. p_arg->p_next = NULL;
  806. /* Check if we can add to the list of arguments for this option */
  807. if( (option < 0) /* Do we have to add to the global list? */
  808. || (cmdline_data.opt_args[option].argc == cmdline_cfg.opts[option].max) /* Did we reach MAX arguments? */
  809. )
  810. {
  811. /* This option does not require arguments. Keep the argument in the global list. */
  812. cmdline_argadd(&(cmdline_data.glb_args), p_arg);
  813. continue;
  814. }
  815. else
  816. {
  817. /* See if the current count has reached max for this option */
  818. if( cmdline_data.opt_args[option].argc == cmdline_cfg.opts[option].max )
  819. {
  820. /* ERROR: too many arguments for an option */
  821. return(CMDLINE_ERR_MANYARG);
  822. }
  823. else
  824. {
  825. /* Link the argument to the arg list of the option */
  826. cmdline_argadd(&(cmdline_data.opt_args[option]), p_arg);
  827. continue;
  828. }
  829. }
  830. }
  831. }
  832. /* ****** We read the complete command line. See if what we collected matches the configuration ******* */
  833. /* Check every collected option against its configuration */
  834. for( i=0; i < 26; i++ )
  835. {
  836. /* Check if this option was allowed */
  837. if(cmdline_cfg.opts[i].flags & CMDLINE_OPTFLAG_ALLOW)
  838. {
  839. /* See if it was mandatory */
  840. if(cmdline_cfg.opts[i].flags & CMDLINE_OPTFLAG_MANDAT)
  841. {
  842. /* Check if we really collected this option on the command line. */
  843. if(cmdline_data.opt_args[i].optc == 0)
  844. {
  845. /* ERROR: a missing mandatory option */
  846. return(CMDLINE_ERR_OPTMIS);
  847. }
  848. else
  849. {
  850. /* Option was there. Check how many args we got for it. */
  851. if(cmdline_data.opt_args[i].argc < cmdline_cfg.opts[i].min)
  852. {
  853. /* ERROR: too few arguments for an option */
  854. return(CMDLINE_ERR_FEWARG);
  855. }
  856. else
  857. {
  858. /* This mandatory option was proper. */
  859. continue;
  860. }
  861. }
  862. }
  863. else /* This is non-mandatory option: */
  864. {
  865. /* Check if the option was specified on the command line */
  866. if(cmdline_data.opt_args[i].optc == 0)
  867. {
  868. /* option wasn't specified, go to the next */
  869. continue;
  870. }
  871. else
  872. {
  873. /* Option was there. Check how many args we collected for it. */
  874. if(cmdline_data.opt_args[i].argc < cmdline_cfg.opts[i].min)
  875. {
  876. /* ERROR: too few arguments for a non-mandatory option */
  877. return(CMDLINE_ERR_FEWARG);
  878. }
  879. else
  880. {
  881. /* This non-mandatory option was proper. */
  882. continue;
  883. }
  884. }
  885. }
  886. }
  887. else /* Option was not allowed. */
  888. {
  889. /* We should not get here as the non-allowed options should have been
  890. trapped eariler. */
  891. }
  892. }
  893. /* Command line was proper as far as the number of options and their arguments */
  894. cmdline_data.parsed = CMDLINE_TRUE;
  895. return(CMDLINE_ERR_OK);
  896. }