1 /* vi: set sw=4 ts=4: */
3 * Copyright (C) 2008, Alpha Networks, Inc.
4 * Created by David Hsieh <david_hsieh@alphanetworks.com>
7 * (SEA)ttle i(MA)ge is the image which used in project seattle.
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
13 * 1. Redistributions of source code must retain the above
14 * copyright notice, this list of conditions and the following
17 * 2. Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials
20 * provided with the distribution.
22 * 3. The name of the author may not be used to endorse or promote
23 * products derived from this software without specific prior
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
27 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
32 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
37 * THE POSSIBILITY OF SUCH DAMAGE.
45 #include <sys/types.h>
49 #include <arpa/inet.h>
54 #define PROGNAME "seama"
55 #define VERSION "0.20"
56 #define MAX_SEAMA_META_SIZE 1024
63 static int o_verbose
= 0; /* verbose mode. */
64 static char * o_dump
= NULL
; /* Seama file to dump. */
65 static char * o_seal
= NULL
; /* Seal the input images when file name exist. */
66 static char * o_extract
= NULL
; /* Extract the seama file. */
67 static char * o_images
[MAX_IMAGE
];/* The image files to pack or seal */
68 static int o_isize
= 0; /* number of images */
69 static char * o_meta
[MAX_META
]; /* meta data array */
70 static int o_msize
= 0; /* size of meta array */
72 static void verbose(const char * format
, ...)
77 va_start(marker
, format
);
78 vfprintf(stdout
, format
, marker
);
83 static void cleanup_exit(int exit_code
)
85 verbose("%s: exit with code %d\n", PROGNAME
, exit_code
);
89 static void show_usage(int exit_code
)
91 printf( PROGNAME
" version " VERSION
"\n"
92 "usage: " PROGNAME
" [OPTIONS]\n"
93 " -h show this help message.\n"
95 " -m {META data} META data.\n"
96 " -d {file} dump the info of the seama file.\n"
97 " -i {input file} image file name.\n"
98 " -s {file} Seal the images to the seama file.\n"
99 " -x {seama file} Extract the seama file.\n"
101 " SEAMA can pack the input file (with -i) into a seama file.\n"
102 " ex: seama -i target.file\n"
103 " SEAMA can also seal multiple seama files into a single seama file.\n"
104 " ex: seama -s final.file -i taget1.seama -i target2.seama\n"
105 " To extract the raw image from SEAMA, you need to specify the meta.\n"
106 " The first image match the specified meta will be extract to\n"
107 " the output file which was specified with '-x'.\n"
108 " ex: seama -x output -i seama.image -m file=sealpac\n"
110 cleanup_exit(exit_code
);
113 static int parse_args(int argc
, char * argv
[])
117 while ((opt
= getopt(argc
, argv
, "hvd:s:i:m:x:")) > 0)
121 default: show_usage(-1); break;
122 case 'h': show_usage(0); break;
123 case 'v': o_verbose
++; break;
124 case 'd': o_dump
= optarg
; break;
125 case 's': o_seal
= optarg
; break;
126 case 'x': o_extract
= optarg
; break;
128 if (o_isize
< MAX_IMAGE
) o_images
[o_isize
++] = optarg
;
129 else printf("Exceed the maximum acceptable image files.!\n");
132 if (o_msize
< MAX_META
) o_meta
[o_msize
++] = optarg
;
133 else printf("Exceed the maximum acceptable META data.!\n");
140 /*******************************************************************/
142 static size_t calculate_digest(FILE * fh
, size_t size
, uint8_t * digest
)
145 size_t bytes_left
, bytes_read
, i
;
146 uint8_t buf
[MAX_SEAMA_META_SIZE
];
148 bytes_left
= size
? size
: sizeof(buf
);
152 while (!feof(fh
) && !ferror(fh
) && bytes_left
> 0)
154 i
= bytes_left
< sizeof(buf
) ? bytes_left
: sizeof(buf
);
155 i
= fread(buf
, sizeof(char), i
, fh
);
158 MD5_Update(&ctx
, buf
, i
);
161 if (size
) bytes_left
-= i
;
163 MD5_Final(digest
, &ctx
);
167 #define READ_BUFF_SIZE 8*1024
168 static size_t copy_file(FILE * to
, FILE * from
)
171 uint8_t buf
[READ_BUFF_SIZE
];
173 while (!feof(from
) && !ferror(from
))
175 i
= fread(buf
, sizeof(uint8_t), READ_BUFF_SIZE
, from
);
179 fwrite(buf
, sizeof(uint8_t), i
, to
);
185 static int verify_seama(const char * fname
, int msg
)
190 uint8_t checksum
[16];
192 uint8_t buf
[MAX_SEAMA_META_SIZE
];
193 size_t msize
, isize
, i
;
196 #define ERRBREAK(fmt, args...) { if (msg) printf(fmt, ##args); break; }
200 if (stat(fname
, &st
) < 0) ERRBREAK("Unable to get the info of '%s'\n",fname
);
201 if ((fh
= fopen(fname
, "r+"))==NULL
) ERRBREAK("Unable to open '%s' for reading!\n",fname
);
203 /* Dump SEAMA header */
204 if (msg
) printf("FILE - %s (%d bytes)\n", fname
, (int)st
.st_size
);
207 while (!feof(fh
) && !ferror(fh
))
210 if (fread(&shdr
, sizeof(shdr
), 1, fh
) != 1) break;
212 /* Check the magic number */
213 if (shdr
.magic
!= htonl(SEAMA_MAGIC
)) ERRBREAK("Invalid SEAMA magic. Probably no more SEAMA!\n");
216 isize
= ntohl(shdr
.size
);
217 msize
= ntohs(shdr
.metasize
);
219 /* The checksum exist only if size is greater than zero. */
222 if (fread(checksum
, sizeof(checksum
), 1, fh
) != 1)
223 ERRBREAK("Error reading checksum !\n");
226 /* Check the META size. */
227 if (msize
> sizeof(buf
)) ERRBREAK("META data in SEAMA header is too large!\n");
229 /* Read META data. */
230 if (fread(buf
, sizeof(char), msize
, fh
) != msize
)
231 ERRBREAK("Unable to read SEAMA META data!\n");
236 printf("SEAMA ==========================================\n");
237 printf(" magic : %08x\n", ntohl(shdr
.magic
));
238 printf(" meta size : %zu bytes\n", msize
);
239 for (i
=0; i
<msize
; i
+=(strlen((const char *)&buf
[i
])+1))
240 printf(" meta data : %s\n", &buf
[i
]);
241 printf(" image size : %zu bytes\n", isize
);
244 /* verify checksum */
249 printf(" checksum : ");
250 for (i
=0; i
<16; i
++) printf("%02X", checksum
[i
]);
254 /* Calculate the checksum */
255 calculate_digest(fh
, isize
, digest
);
258 printf(" digest : ");
259 for (i
=0; i
<16; i
++) printf("%02X", digest
[i
]);
263 if (memcmp(checksum
, digest
, 16)!=0) ERRBREAK("!!ERROR!! checksum error !!\n");
267 if (msg
) printf("================================================\n");
273 static size_t write_seama_header(FILE * fh
, char * meta
[], size_t msize
, size_t size
)
277 uint16_t metasize
= 0;
279 /* Calculate the META size */
280 for (i
=0; i
<msize
; i
++) metasize
+= (strlen(meta
[i
]) + 1);
281 //+++ let meta data end on 4 alignment by siyou. 2010/3/1 03:58pm
282 metasize
= ((metasize
+3)/4)*4;
283 verbose("SEAMA META : %d bytes\n", metasize
);
285 /* Fill up the header, all the data endian should be network byte order. */
286 shdr
.magic
= htonl(SEAMA_MAGIC
);
288 shdr
.metasize
= htons(metasize
);
289 shdr
.size
= htonl(size
);
291 /* Write the header */
292 return fwrite(&shdr
, sizeof(seamahdr_t
), 1, fh
);
295 static size_t write_checksum(FILE * fh
, uint8_t * checksum
)
297 return fwrite(checksum
, sizeof(uint8_t), 16, fh
);
300 static size_t write_meta_data(FILE * fh
, char * meta
[], size_t size
)
305 for (i
=0; i
<size
; i
++)
307 verbose("SEAMA META data : %s\n", meta
[i
]);
308 j
= fwrite(meta
[i
], sizeof(char), strlen(meta
[i
])+1, fh
);
309 if (j
!= strlen(meta
[i
])+1) return 0;
312 //+++ let meta data end on 4 alignment by siyou. 2010/3/1 03:58pm
314 for ( ; ret
< j
; ret
++)
315 fwrite("", sizeof(char), 1, fh
);
320 /*******************************************************************/
322 static void dump_seama(const char * fname
)
324 verify_seama(fname
, 1);
327 static void seal_files(const char * file
)
333 /* Each image should be seama. */
334 for (i
= 0; i
< o_isize
; i
++)
336 if (verify_seama(o_images
[i
], 0) < 0)
338 printf("'%s' is not a seama file !\n",o_images
[i
]);
343 /* Open file for write */
344 fh
= fopen(file
, "w+");
347 /* Write the header. */
348 write_seama_header(fh
, o_meta
, o_msize
, 0);
349 write_meta_data(fh
, o_meta
, o_msize
);
351 /* Write image files */
352 for (i
=0; i
<o_isize
; i
++)
354 ifh
= fopen(o_images
[i
], "r+");
366 static void pack_files(void)
374 for (i
=0; i
<o_isize
; i
++)
376 /* Open the input file. */
377 ifh
= fopen(o_images
[i
], "r+");
380 fsize
= calculate_digest(ifh
, 0, digest
);
381 verbose("file size (%s) : %d\n", o_images
[i
], fsize
);
384 /* Open the output file. */
385 sprintf(filename
, "%s.seama", o_images
[i
]);
386 fh
= fopen(filename
, "w+");
389 write_seama_header(fh
, o_meta
, o_msize
, fsize
);
390 write_checksum(fh
, digest
);
391 write_meta_data(fh
, o_meta
, o_msize
);
399 printf("Unable to open image file '%s'\n",o_images
[i
]);
404 /**************************************************************************/
406 static int match_meta(const char * meta
, size_t size
)
411 for (i
= 0; i
< o_msize
; i
++)
413 for (match
= 0, j
= 0; j
< size
; j
+= (strlen(&meta
[j
])+1))
414 if (strcmp(&meta
[j
], o_meta
[i
])==0) { match
++; break; }
415 if (!match
) return 0;
421 static void extract_file(const char * output
)
425 size_t msize
, isize
, i
, m
;
427 uint8_t buf
[MAX_SEAMA_META_SIZE
];
430 /* We need meta for searching the target image. */
433 printf("SEAMA: need meta for searching image.\n");
437 /* Walk through each input file */
438 for (i
= 0; i
< o_isize
; i
++)
440 /* verify the input file */
441 if (verify_seama(o_images
[i
], 0) < 0)
443 printf("SEAMA: '%s' is not a seama file !\n", o_images
[i
]);
446 /* open the input file */
447 ifh
= fopen(o_images
[i
], "r");
450 while (!feof(ifh
) && !ferror(ifh
))
453 fread(&shdr
, sizeof(shdr
), 1, ifh
);
454 if (shdr
.magic
!= htonl(SEAMA_MAGIC
)) break;
456 isize
= ntohl(shdr
.size
);
457 msize
= ntohs(shdr
.metasize
);
462 m
= fread(buf
, sizeof(char), (msize
< MAX_SEAMA_META_SIZE
) ? msize
: MAX_SEAMA_META_SIZE
, ifh
);
469 fread(buf
, sizeof(char), 16, ifh
);
473 fread(buf
, sizeof(char), msize
, ifh
);
474 if (match_meta((const char *)buf
, msize
))
476 printf("SEAMA: found image @ '%s', image size: %zu\n", o_images
[i
], isize
);
477 /* open output file */
478 ofh
= fopen(output
, "w");
479 if (!ofh
) printf("SEAMA: unable to open '%s' for writting.\n",output
);
484 m
= fread(buf
, sizeof(char), (isize
< MAX_SEAMA_META_SIZE
) ? isize
: MAX_SEAMA_META_SIZE
, ifh
);
486 fwrite(buf
, sizeof(char), m
, ofh
);
497 m
= fread(buf
, sizeof(char), (isize
< MAX_SEAMA_META_SIZE
) ? isize
: MAX_SEAMA_META_SIZE
, ifh
);
502 /* close the file. */
509 /*******************************************************************/
511 int seama_main(int argc
, char * argv
[], char * env
[])
513 int main(int argc
, char * argv
[], char * env
[])
516 verbose("SEAMA version " VERSION
"\n");
518 /* parse the arguments */
519 if (parse_args(argc
, argv
) < 0) show_usage(9);
522 if (o_dump
) dump_seama(o_dump
);
523 else if (o_seal
) seal_files(o_seal
);
524 else if (o_extract
) extract_file(o_extract
);