1 // SPDX-License-Identifier: BSD-3-Clause
2 /* vi: set sw=4 ts=4: */
4 * Copyright (C) 2008, Alpha Networks, Inc.
5 * Created by David Hsieh <david_hsieh@alphanetworks.com>
8 * (SEA)ttle i(MA)ge is the image which used in project seattle.
15 #include <sys/types.h>
19 #include <arpa/inet.h>
24 #define PROGNAME "seama"
25 #define VERSION "0.20"
26 #define MAX_SEAMA_META_SIZE 1024
33 static int o_verbose
= 0; /* verbose mode. */
34 static char * o_dump
= NULL
; /* Seama file to dump. */
35 static char * o_seal
= NULL
; /* Seal the input images when file name exist. */
36 static char * o_extract
= NULL
; /* Extract the seama file. */
37 static char * o_images
[MAX_IMAGE
];/* The image files to pack or seal */
38 static int o_isize
= 0; /* number of images */
39 static char * o_meta
[MAX_META
]; /* meta data array */
40 static int o_msize
= 0; /* size of meta array */
42 static void verbose(const char * format
, ...)
47 va_start(marker
, format
);
48 vfprintf(stdout
, format
, marker
);
53 static void cleanup_exit(int exit_code
)
55 verbose("%s: exit with code %d\n", PROGNAME
, exit_code
);
59 static void show_usage(int exit_code
)
61 printf( PROGNAME
" version " VERSION
"\n"
62 "usage: " PROGNAME
" [OPTIONS]\n"
63 " -h show this help message.\n"
65 " -m {META data} META data.\n"
66 " -d {file} dump the info of the seama file.\n"
67 " -i {input file} image file name.\n"
68 " -s {file} Seal the images to the seama file.\n"
69 " -x {seama file} Extract the seama file.\n"
71 " SEAMA can pack the input file (with -i) into a seama file.\n"
72 " ex: seama -i target.file\n"
73 " SEAMA can also seal multiple seama files into a single seama file.\n"
74 " ex: seama -s final.file -i taget1.seama -i target2.seama\n"
75 " To extract the raw image from SEAMA, you need to specify the meta.\n"
76 " The first image match the specified meta will be extract to\n"
77 " the output file which was specified with '-x'.\n"
78 " ex: seama -x output -i seama.image -m file=sealpac\n"
80 cleanup_exit(exit_code
);
83 static int parse_args(int argc
, char * argv
[])
87 while ((opt
= getopt(argc
, argv
, "hvd:s:i:m:x:")) > 0)
91 default: show_usage(-1); break;
92 case 'h': show_usage(0); break;
93 case 'v': o_verbose
++; break;
94 case 'd': o_dump
= optarg
; break;
95 case 's': o_seal
= optarg
; break;
96 case 'x': o_extract
= optarg
; break;
98 if (o_isize
< MAX_IMAGE
) o_images
[o_isize
++] = optarg
;
99 else printf("Exceed the maximum acceptable image files.!\n");
102 if (o_msize
< MAX_META
) o_meta
[o_msize
++] = optarg
;
103 else printf("Exceed the maximum acceptable META data.!\n");
110 /*******************************************************************/
112 static size_t calculate_digest(FILE * fh
, size_t size
, uint8_t * digest
)
115 size_t bytes_left
, bytes_read
, i
;
116 uint8_t buf
[MAX_SEAMA_META_SIZE
];
118 bytes_left
= size
? size
: sizeof(buf
);
122 while (!feof(fh
) && !ferror(fh
) && bytes_left
> 0)
124 i
= bytes_left
< sizeof(buf
) ? bytes_left
: sizeof(buf
);
125 i
= fread(buf
, sizeof(char), i
, fh
);
128 MD5_Update(&ctx
, buf
, i
);
131 if (size
) bytes_left
-= i
;
133 MD5_Final(digest
, &ctx
);
137 #define READ_BUFF_SIZE 8*1024
138 static size_t copy_file(FILE * to
, FILE * from
)
141 uint8_t buf
[READ_BUFF_SIZE
];
143 while (!feof(from
) && !ferror(from
))
145 i
= fread(buf
, sizeof(uint8_t), READ_BUFF_SIZE
, from
);
149 fwrite(buf
, sizeof(uint8_t), i
, to
);
155 static int verify_seama(const char * fname
, int msg
)
160 uint8_t checksum
[16];
162 uint8_t buf
[MAX_SEAMA_META_SIZE
];
163 size_t msize
, isize
, i
;
166 #define ERRBREAK(fmt, args...) { if (msg) printf(fmt, ##args); break; }
170 if (stat(fname
, &st
) < 0) ERRBREAK("Unable to get the info of '%s'\n",fname
);
171 if ((fh
= fopen(fname
, "r+"))==NULL
) ERRBREAK("Unable to open '%s' for reading!\n",fname
);
173 /* Dump SEAMA header */
174 if (msg
) printf("FILE - %s (%d bytes)\n", fname
, (int)st
.st_size
);
177 while (!feof(fh
) && !ferror(fh
))
180 if (fread(&shdr
, sizeof(shdr
), 1, fh
) != 1) break;
182 /* Check the magic number */
183 if (shdr
.magic
!= htonl(SEAMA_MAGIC
)) ERRBREAK("Invalid SEAMA magic. Probably no more SEAMA!\n");
186 isize
= ntohl(shdr
.size
);
187 msize
= ntohs(shdr
.metasize
);
189 /* The checksum exist only if size is greater than zero. */
192 if (fread(checksum
, sizeof(checksum
), 1, fh
) != 1)
193 ERRBREAK("Error reading checksum !\n");
196 /* Check the META size. */
197 if (msize
> sizeof(buf
)) ERRBREAK("META data in SEAMA header is too large!\n");
199 /* Read META data. */
200 if (fread(buf
, sizeof(char), msize
, fh
) != msize
)
201 ERRBREAK("Unable to read SEAMA META data!\n");
206 printf("SEAMA ==========================================\n");
207 printf(" magic : %08x\n", ntohl(shdr
.magic
));
208 printf(" meta size : %zu bytes\n", msize
);
209 for (i
=0; i
<msize
; i
+=(strlen((const char *)&buf
[i
])+1))
210 printf(" meta data : %s\n", &buf
[i
]);
211 printf(" image size : %zu bytes\n", isize
);
214 /* verify checksum */
219 printf(" checksum : ");
220 for (i
=0; i
<16; i
++) printf("%02X", checksum
[i
]);
224 /* Calculate the checksum */
225 calculate_digest(fh
, isize
, digest
);
228 printf(" digest : ");
229 for (i
=0; i
<16; i
++) printf("%02X", digest
[i
]);
233 if (memcmp(checksum
, digest
, 16)!=0) ERRBREAK("!!ERROR!! checksum error !!\n");
237 if (msg
) printf("================================================\n");
243 static size_t write_seama_header(FILE * fh
, char * meta
[], size_t msize
, size_t size
)
247 uint16_t metasize
= 0;
249 /* Calculate the META size */
250 for (i
=0; i
<msize
; i
++) metasize
+= (strlen(meta
[i
]) + 1);
251 //+++ let meta data end on 4 alignment by siyou. 2010/3/1 03:58pm
252 metasize
= ((metasize
+3)/4)*4;
253 verbose("SEAMA META : %d bytes\n", metasize
);
255 /* Fill up the header, all the data endian should be network byte order. */
256 shdr
.magic
= htonl(SEAMA_MAGIC
);
258 shdr
.metasize
= htons(metasize
);
259 shdr
.size
= htonl(size
);
261 /* Write the header */
262 return fwrite(&shdr
, sizeof(seamahdr_t
), 1, fh
);
265 static size_t write_checksum(FILE * fh
, uint8_t * checksum
)
267 return fwrite(checksum
, sizeof(uint8_t), 16, fh
);
270 static size_t write_meta_data(FILE * fh
, char * meta
[], size_t size
)
275 for (i
=0; i
<size
; i
++)
277 verbose("SEAMA META data : %s\n", meta
[i
]);
278 j
= fwrite(meta
[i
], sizeof(char), strlen(meta
[i
])+1, fh
);
279 if (j
!= strlen(meta
[i
])+1) return 0;
282 //+++ let meta data end on 4 alignment by siyou. 2010/3/1 03:58pm
284 for ( ; ret
< j
; ret
++)
285 fwrite("", sizeof(char), 1, fh
);
290 /*******************************************************************/
292 static void dump_seama(const char * fname
)
294 verify_seama(fname
, 1);
297 static void seal_files(const char * file
)
303 /* Each image should be seama. */
304 for (i
= 0; i
< o_isize
; i
++)
306 if (verify_seama(o_images
[i
], 0) < 0)
308 printf("'%s' is not a seama file !\n",o_images
[i
]);
313 /* Open file for write */
314 fh
= fopen(file
, "w+");
317 /* Write the header. */
318 write_seama_header(fh
, o_meta
, o_msize
, 0);
319 write_meta_data(fh
, o_meta
, o_msize
);
321 /* Write image files */
322 for (i
=0; i
<o_isize
; i
++)
324 ifh
= fopen(o_images
[i
], "r+");
336 static void pack_files(void)
344 for (i
=0; i
<o_isize
; i
++)
346 /* Open the input file. */
347 ifh
= fopen(o_images
[i
], "r+");
350 fsize
= calculate_digest(ifh
, 0, digest
);
351 verbose("file size (%s) : %d\n", o_images
[i
], fsize
);
354 /* Open the output file. */
355 sprintf(filename
, "%s.seama", o_images
[i
]);
356 fh
= fopen(filename
, "w+");
359 write_seama_header(fh
, o_meta
, o_msize
, fsize
);
360 write_checksum(fh
, digest
);
361 write_meta_data(fh
, o_meta
, o_msize
);
369 printf("Unable to open image file '%s'\n",o_images
[i
]);
374 /**************************************************************************/
376 static int match_meta(const char * meta
, size_t size
)
381 for (i
= 0; i
< o_msize
; i
++)
383 for (match
= 0, j
= 0; j
< size
; j
+= (strlen(&meta
[j
])+1))
384 if (strcmp(&meta
[j
], o_meta
[i
])==0) { match
++; break; }
385 if (!match
) return 0;
391 static void extract_file(const char * output
)
395 size_t msize
, isize
, i
, m
;
397 uint8_t buf
[MAX_SEAMA_META_SIZE
];
400 /* We need meta for searching the target image. */
403 printf("SEAMA: need meta for searching image.\n");
407 /* Walk through each input file */
408 for (i
= 0; i
< o_isize
; i
++)
410 /* verify the input file */
411 if (verify_seama(o_images
[i
], 0) < 0)
413 printf("SEAMA: '%s' is not a seama file !\n", o_images
[i
]);
416 /* open the input file */
417 ifh
= fopen(o_images
[i
], "r");
420 while (!feof(ifh
) && !ferror(ifh
))
423 fread(&shdr
, sizeof(shdr
), 1, ifh
);
424 if (shdr
.magic
!= htonl(SEAMA_MAGIC
)) break;
426 isize
= ntohl(shdr
.size
);
427 msize
= ntohs(shdr
.metasize
);
432 m
= fread(buf
, sizeof(char), (msize
< MAX_SEAMA_META_SIZE
) ? msize
: MAX_SEAMA_META_SIZE
, ifh
);
439 fread(buf
, sizeof(char), 16, ifh
);
443 fread(buf
, sizeof(char), msize
, ifh
);
444 if (match_meta((const char *)buf
, msize
))
446 printf("SEAMA: found image @ '%s', image size: %zu\n", o_images
[i
], isize
);
447 /* open output file */
448 ofh
= fopen(output
, "w");
449 if (!ofh
) printf("SEAMA: unable to open '%s' for writting.\n",output
);
454 m
= fread(buf
, sizeof(char), (isize
< MAX_SEAMA_META_SIZE
) ? isize
: MAX_SEAMA_META_SIZE
, ifh
);
456 fwrite(buf
, sizeof(char), m
, ofh
);
467 m
= fread(buf
, sizeof(char), (isize
< MAX_SEAMA_META_SIZE
) ? isize
: MAX_SEAMA_META_SIZE
, ifh
);
472 /* close the file. */
479 /*******************************************************************/
481 int seama_main(int argc
, char * argv
[], char * env
[])
483 int main(int argc
, char * argv
[], char * env
[])
486 verbose("SEAMA version " VERSION
"\n");
488 /* parse the arguments */
489 if (parse_args(argc
, argv
) < 0) show_usage(9);
492 if (o_dump
) dump_seama(o_dump
);
493 else if (o_seal
) seal_files(o_seal
);
494 else if (o_extract
) extract_file(o_extract
);