• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/firmware-utils/src/uimage_sgehdr.c

  1 // SPDX-License-Identifier: GPL-2.0-or-later
  2 /*
  3  * uimage_sgehdr.c : add 96 bytes of extra header information after the normal tail of uimage header
  4  * this is an edited version of uimage_padhdr.c
  5  *
  6  * Copyright (C) 2019 NOGUCHI Hiroshi <drvlabo@gmail.com>
  7  */
  8 
  9 #include <stdio.h>
 10 #include <errno.h>
 11 #include <unistd.h>
 12 #include <stdlib.h>
 13 #include <string.h>
 14 #include <fcntl.h>
 15 #include <sys/stat.h>
 16 #include <arpa/inet.h>
 17 #include <zlib.h>
 18 
 19 
 20 /* from u-boot/include/image.h */
 21 #define IH_NMLEN                32      /* Image Name Length            */
 22 #define SGE_PRODUCTLEN          64      /* sge_Product Length           */
 23 #define SGE_VERSIONLEN          16      /* sge Version Length           */
 24 #define OrignalHL               64      /* Original Header Length       */
 25 
 26 /*
 27  * SGE format image header,
 28  * all data in network byte order (aka natural aka bigendian).
 29  */
 30 struct image_header {
 31         uint32_t        ih_magic;       /* Image Header Magic Number    */
 32         uint32_t        ih_hcrc;        /* Image Header CRC Checksum    */
 33         uint32_t        ih_time;        /* Image Creation Timestamp     */
 34         uint32_t        ih_size;        /* Image Data Size              */
 35         uint32_t        ih_load;        /* Data  Load  Address          */
 36         uint32_t        ih_ep;          /* Entry Point Address          */
 37         uint32_t        ih_dcrc;        /* Image Data CRC Checksum      */
 38         uint8_t         ih_os;          /* Operating System             */
 39         uint8_t         ih_arch;        /* CPU architecture             */
 40         uint8_t         ih_type;        /* Image Type                   */
 41         uint8_t         ih_comp;        /* Compression Type             */
 42         uint8_t         ih_name[IH_NMLEN];      /* Image Name           */
 43         char            sgeih_p[SGE_PRODUCTLEN];        /* sge_Product          */
 44         char            sgeih_sv[SGE_VERSIONLEN];       /* sge Software Version         */
 45         char            sgeih_hv[SGE_VERSIONLEN];       /* sge Hardware Version         */
 46 };
 47 
 48 
 49 /* default padding size */
 50 #define IH_PAD_BYTES            (96)
 51 
 52 
 53 static void usage(char *prog)
 54 {
 55         fprintf(stderr,
 56                 "%s -i <input_uimage_file> -o <output_file> -m <model> -h <hardware version> -s <software version>\n",
 57                 prog);
 58 }
 59 
 60 int main(int argc, char *argv[])
 61 {
 62         struct stat statbuf;
 63         u_int8_t *filebuf;
 64         int ifd;
 65         int ofd;
 66         ssize_t rsz;
 67         u_int32_t crc_recalc;
 68         struct image_header *imgh;
 69         int opt;
 70         char *infname = NULL;
 71         char *outfname = NULL;
 72         char *model = NULL;
 73         char *hversion = NULL;
 74         char *sversion = NULL;
 75         int padsz = IH_PAD_BYTES;
 76 
 77         while ((opt = getopt(argc, argv, "i:o:m:h:s:")) != -1) {
 78                 switch (opt) {
 79                 case 'i':
 80                         infname = optarg;
 81                         break;
 82                 case 'o':
 83                         outfname = optarg;
 84                         break;
 85                 case 'm':
 86                         model = optarg;
 87                         break;
 88                 case 'h':
 89                         hversion = optarg;
 90                         break;
 91                 case 's':
 92                         sversion = optarg;
 93                         break;
 94                 default:
 95                         break;
 96                 }
 97         }
 98 
 99         if (!infname || !outfname) {
100                 usage(argv[0]);
101                 exit(1);
102         }
103 
104         ifd = open(infname, O_RDONLY);
105         if (ifd < 0) {
106                 fprintf(stderr,
107                         "could not open input file. (errno = %d)\n", errno);
108                 exit(1);
109         }
110 
111         ofd = open(outfname, O_WRONLY | O_CREAT, 0644);
112         if (ofd < 0) {
113                 fprintf(stderr,
114                         "could not open output file. (errno = %d)\n", errno);
115                 exit(1);
116         }
117 
118         if (fstat(ifd, &statbuf) < 0) {
119                 fprintf(stderr,
120                         "could not fstat input file. (errno = %d)\n", errno);
121                 exit(1);
122         }
123 
124         filebuf = malloc(statbuf.st_size + padsz);
125         if (!filebuf) {
126                 fprintf(stderr, "buffer allocation failed\n");
127                 exit(1);
128         }
129 
130         rsz = read(ifd, filebuf, OrignalHL);
131         if (rsz != OrignalHL) {
132                 fprintf(stderr,
133                         "could not read input file (errno = %d).\n", errno);
134                 exit(1);
135         }
136 
137         memset(&(filebuf[OrignalHL]), 0, padsz);
138 
139         rsz = read(ifd, &(filebuf[sizeof(*imgh)]),
140                                 statbuf.st_size - OrignalHL);
141         if (rsz != (int32_t)(statbuf.st_size - OrignalHL)) {
142                 fprintf(stderr,
143                         "could not read input file (errno = %d).\n", errno);
144                 exit(1);
145         }
146 
147         imgh = (struct image_header *)filebuf;
148 
149         imgh->ih_hcrc = 0;
150 
151         strncpy(imgh->sgeih_p, model, sizeof(imgh->sgeih_p));
152         strncpy(imgh->sgeih_sv, sversion, sizeof(imgh->sgeih_sv));
153         strncpy(imgh->sgeih_hv, hversion, sizeof(imgh->sgeih_hv));
154 
155         crc_recalc = crc32(0, filebuf, sizeof(*imgh));
156         imgh->ih_hcrc = htonl(crc_recalc);
157 
158         rsz = write(ofd, filebuf, statbuf.st_size + padsz);
159         if (rsz != (int32_t)statbuf.st_size + padsz) {
160                 fprintf(stderr,
161                         "could not write output file (errnor = %d).\n", errno);
162                 exit(1);
163         }
164 
165         return 0;
166 }
167 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt