📄 mkimage.c
字号:
* Must be -w then: * * write dummy header, to be fixed later */ memset (hdr, 0, sizeof(image_header_t)); if (write(ifd, hdr, sizeof(image_header_t)) != sizeof(image_header_t)) { fprintf (stderr, "%s: Write error on %s: %s\n", cmdname, imagefile, strerror(errno)); exit (EXIT_FAILURE); } if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) { char *file = datafile; unsigned long size; for (;;) { char *sep = NULL; if (file) { if ((sep = strchr(file, ':')) != NULL) { *sep = '\0'; } if (stat (file, &sbuf) < 0) { fprintf (stderr, "%s: Can't stat %s: %s\n", cmdname, file, strerror(errno)); exit (EXIT_FAILURE); } size = htonl(sbuf.st_size); } else { size = 0; } if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) { fprintf (stderr, "%s: Write error on %s: %s\n", cmdname, imagefile, strerror(errno)); exit (EXIT_FAILURE); } if (!file) { break; } if (sep) { *sep = ':'; file = sep + 1; } else { file = NULL; } } file = datafile; for (;;) { char *sep = strchr(file, ':'); if (sep) { *sep = '\0'; copy_file (ifd, file, 1); *sep++ = ':'; file = sep; } else { copy_file (ifd, file, 0); break; } } } else { copy_file (ifd, datafile, 0); } /* We're a bit of paranoid */#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) (void) fdatasync (ifd);#else (void) fsync (ifd);#endif if (fstat(ifd, &sbuf) < 0) { fprintf (stderr, "%s: Can't stat %s: %s\n", cmdname, imagefile, strerror(errno)); exit (EXIT_FAILURE); } ptr = (unsigned char *)mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0); if (ptr == MAP_FAILED) { fprintf (stderr, "%s: Can't map %s: %s\n", cmdname, imagefile, strerror(errno)); exit (EXIT_FAILURE); } hdr = (image_header_t *)ptr; checksum = crc32 (0, (const char *)(ptr + sizeof(image_header_t)), sbuf.st_size - sizeof(image_header_t) ); /* Build new header */ hdr->ih_magic = htonl(IH_MAGIC); hdr->ih_time = htonl(sbuf.st_mtime); hdr->ih_size = htonl(sbuf.st_size - sizeof(image_header_t)); hdr->ih_load = htonl(addr); hdr->ih_ep = htonl(ep); hdr->ih_dcrc = htonl(checksum); hdr->ih_os = opt_os; hdr->ih_arch = opt_arch; hdr->ih_type = opt_type; hdr->ih_comp = opt_comp; strncpy((char *)hdr->ih_name, name, IH_NMLEN); checksum = crc32(0,(const char *)hdr,sizeof(image_header_t)); hdr->ih_hcrc = htonl(checksum); print_header (hdr); (void) munmap((void *)ptr, sbuf.st_size); if (close(ifd)) { fprintf (stderr, "%s: Write error on %s: %s\n", cmdname, imagefile, strerror(errno)); exit (EXIT_FAILURE); } exit (EXIT_SUCCESS);}static voidcopy_file (int ifd, const char *datafile, int pad){ int dfd; struct stat sbuf; unsigned char *ptr; int tail; int zero = 0; if (vflag) { fprintf (stderr, "Adding Image %s\n", datafile); } if ((dfd = open(datafile, O_RDONLY)) < 0) { fprintf (stderr, "%s: Can't open %s: %s\n", cmdname, datafile, strerror(errno)); exit (EXIT_FAILURE); } if (fstat(dfd, &sbuf) < 0) { fprintf (stderr, "%s: Can't stat %s: %s\n", cmdname, datafile, strerror(errno)); exit (EXIT_FAILURE); } ptr = (unsigned char *)mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); if (ptr == MAP_FAILED) { fprintf (stderr, "%s: Can't read %s: %s\n", cmdname, datafile, strerror(errno)); exit (EXIT_FAILURE); } if (write(ifd, ptr, sbuf.st_size) != sbuf.st_size) { fprintf (stderr, "%s: Write error on %s: %s\n", cmdname, imagefile, strerror(errno)); exit (EXIT_FAILURE); } if (pad && ((tail = sbuf.st_size % 4) != 0)) { if (write(ifd, (char *)&zero, 4-tail) != 4-tail) { fprintf (stderr, "%s: Write error on %s: %s\n", cmdname, imagefile, strerror(errno)); exit (EXIT_FAILURE); } } (void) munmap((void *)ptr, sbuf.st_size); (void) close (dfd);}voidusage (){ fprintf (stderr, "Usage: %s -l image\n" " -l ==> list image header information\n" " %s -A arch -O os -T type -C comp " "-a addr -e ep -n name -d data_file[:data_file...] image\n", cmdname, cmdname); fprintf (stderr, " -A ==> set architecture to 'arch'\n" " -O ==> set operating system to 'os'\n" " -T ==> set image type to 'type'\n" " -C ==> set compression type 'comp'\n" " -a ==> set load address to 'addr' (hex)\n" " -e ==> set entry point to 'ep' (hex)\n" " -n ==> set image name to 'name'\n" " -d ==> use image data from 'datafile'\n" ); exit (EXIT_FAILURE);}static voidprint_header (image_header_t *hdr){ time_t timestamp; uint32_t size; timestamp = (time_t)ntohl(hdr->ih_time); size = ntohl(hdr->ih_size); printf ("Image Name: %.*s\n", IH_NMLEN, hdr->ih_name); printf ("Created: %s", ctime(×tamp)); printf ("Image Type: "); print_type(hdr); printf ("Data Size: %d Bytes = %.2f kB = %.2f MB\n", size, (double)size / 1.024e3, (double)size / 1.048576e6 ); printf ("Load Address: 0x%08x\n", ntohl(hdr->ih_load)); printf ("Entry Point: 0x%08x\n", ntohl(hdr->ih_ep)); if (hdr->ih_type == IH_TYPE_MULTI || hdr->ih_type == IH_TYPE_SCRIPT) { int i, ptrs; uint32_t pos; unsigned long *len_ptr = (unsigned long *) ( (unsigned long)hdr + sizeof(image_header_t) ); /* determine number of images first (to calculate image offsets) */ for (i=0; len_ptr[i]; ++i) /* null pointer terminates list */ ; ptrs = i; /* null pointer terminates list */ pos = sizeof(image_header_t) + ptrs * sizeof(long); printf ("Contents:\n"); for (i=0; len_ptr[i]; ++i) { size = ntohl(len_ptr[i]); printf (" Image %d: %8d Bytes = %4d kB = %d MB\n", i, size, size>>10, size>>20); if (hdr->ih_type == IH_TYPE_SCRIPT && i > 0) { /* * the user may need to know offsets * if planning to do something with * multiple files */ printf (" Offset = %08x\n", pos); } /* copy_file() will pad the first files to even word align */ size += 3; size &= ~3; pos += size; } }}static voidprint_type (image_header_t *hdr){ printf ("%s %s %s (%s)\n", put_arch (hdr->ih_arch), put_os (hdr->ih_os ), put_type (hdr->ih_type), put_comp (hdr->ih_comp) );}static char *put_arch (int arch){ return (put_table_entry(arch_name, "Unknown Architecture", arch));}static char *put_os (int os){ return (put_table_entry(os_name, "Unknown OS", os));}static char *put_type (int type){ return (put_table_entry(type_name, "Unknown Image", type));}static char *put_comp (int comp){ return (put_table_entry(comp_name, "Unknown Compression", comp));}static char *put_table_entry (table_entry_t *table, char *msg, int type){ for (; table->val>=0; ++table) { if (table->val == type) return (table->lname); } return (msg);}static int get_arch(char *name){ return (get_table_entry(arch_name, "CPU", name));}static int get_comp(char *name){ return (get_table_entry(comp_name, "Compression", name));}static int get_os (char *name){ return (get_table_entry(os_name, "OS", name));}static int get_type(char *name){ return (get_table_entry(type_name, "Image", name));}static int get_table_entry (table_entry_t *table, char *msg, char *name){ table_entry_t *t; int first = 1; for (t=table; t->val>=0; ++t) { if (t->sname && strcasecmp(t->sname, name)==0) return (t->val); } fprintf (stderr, "\nInvalid %s Type - valid names are", msg); for (t=table; t->val>=0; ++t) { if (t->sname == NULL) continue; fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname); first = 0; } fprintf (stderr, "\n"); return (-1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -