1 // SPDX-License-Identifier: GPL-2.0-or-later
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
6 * Copyright (C) 2019 NOGUCHI Hiroshi <drvlabo@gmail.com>
16 #include <arpa/inet.h>
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 */
27 * SGE format image header,
28 * all data in network byte order (aka natural aka bigendian).
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 uint8_t sgeih_p
[SGE_PRODUCTLEN
]; /* sge_Product */
44 uint8_t sgeih_sv
[SGE_VERSIONLEN
]; /* sge Software Version */
45 uint8_t sgeih_hv
[SGE_VERSIONLEN
]; /* sge Hardware Version */
49 /* default padding size */
50 #define IH_PAD_BYTES (96)
53 static void usage(char *prog
)
56 "%s -i <input_uimage_file> -o <output_file> -m <model> -h <hardware version> -s <software version>\n",
60 int main(int argc
, char *argv
[])
68 struct image_header
*imgh
;
71 char *outfname
= NULL
;
73 char *hversion
= NULL
;
74 char *sversion
= NULL
;
75 int padsz
= IH_PAD_BYTES
;
78 while ((opt
= getopt(argc
, argv
, "i:o:m:h:s:")) != -1) {
100 if (!infname
|| !outfname
) {
105 ifd
= open(infname
, O_RDONLY
);
108 "could not open input file. (errno = %d)\n", errno
);
112 ofd
= open(outfname
, O_WRONLY
| O_CREAT
, 0644);
115 "could not open output file. (errno = %d)\n", errno
);
119 if (fstat(ifd
, &statbuf
) < 0) {
121 "could not fstat input file. (errno = %d)\n", errno
);
125 filebuf
= malloc(statbuf
.st_size
+ padsz
);
127 fprintf(stderr
, "buffer allocation failed\n");
131 rsz
= read(ifd
, filebuf
, OrignalHL
);
132 if (rsz
!= OrignalHL
) {
134 "could not read input file (errno = %d).\n", errno
);
138 memset(&(filebuf
[OrignalHL
]), 0, padsz
);
140 rsz
= read(ifd
, &(filebuf
[sizeof(*imgh
)]),
141 statbuf
.st_size
- OrignalHL
);
142 if (rsz
!= (int32_t)(statbuf
.st_size
- OrignalHL
)) {
144 "could not read input file (errno = %d).\n", errno
);
148 imgh
= (struct image_header
*)filebuf
;
152 strncpy(imgh
->sgeih_p
, model
, sizeof(imgh
->sgeih_p
));
153 strncpy(imgh
->sgeih_sv
, sversion
, sizeof(imgh
->sgeih_sv
));
154 strncpy(imgh
->sgeih_hv
, hversion
, sizeof(imgh
->sgeih_hv
));
156 crc_recalc
= crc32(0, filebuf
, sizeof(*imgh
));
157 imgh
->ih_hcrc
= htonl(crc_recalc
);
159 rsz
= write(ofd
, filebuf
, statbuf
.st_size
+ padsz
);
160 if (rsz
!= (int32_t)statbuf
.st_size
+ padsz
) {
162 "could not write output file (errnor = %d).\n", errno
);