1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2016 Rafał Miłecki <zajec5@gmail.com>
19 #if !defined(__BYTE_ORDER)
20 #error "Unknown byte order"
23 #if __BYTE_ORDER == __BIG_ENDIAN
24 #define cpu_to_be32(x) (x)
25 #define be32_to_cpu(x) (x)
26 #define cpu_to_be16(x) (x)
27 #define be16_to_cpu(x) (x)
28 #elif __BYTE_ORDER == __LITTLE_ENDIAN
29 #define cpu_to_be32(x) bswap_32(x)
30 #define be32_to_cpu(x) bswap_32(x)
31 #define cpu_to_be16(x) bswap_16(x)
32 #define be16_to_cpu(x) bswap_16(x)
34 #error "Unsupported endianness"
37 #define SEAMA_MAGIC 0x5ea3a417
39 struct seama_seal_header
{
44 } __attribute__ ((packed
));
46 struct seama_entity_header
{
52 } __attribute__ ((packed
));
58 static inline size_t oseama_min(size_t x
, size_t y
) {
62 /**************************************************
64 **************************************************/
66 static void oseama_info_parse_options(int argc
, char **argv
) {
69 while ((c
= getopt(argc
, argv
, "e:")) != -1) {
72 entity_idx
= atoi(optarg
);
78 static int oseama_info_entities(FILE *seama
) {
79 struct seama_entity_header hdr
;
80 size_t bytes
, metasize
, imagesize
;
86 while ((bytes
= fread(&hdr
, 1, sizeof(hdr
), seama
)) == sizeof(hdr
)) {
87 if (be32_to_cpu(hdr
.magic
) != SEAMA_MAGIC
) {
88 fprintf(stderr
, "Invalid Seama magic: 0x%08x\n", be32_to_cpu(hdr
.magic
));
92 metasize
= be16_to_cpu(hdr
.metasize
);
93 imagesize
= be32_to_cpu(hdr
.imagesize
);
95 if (entity_idx
>= 0 && i
!= entity_idx
) {
96 fseek(seama
, metasize
+ imagesize
, SEEK_CUR
);
101 if (metasize
>= sizeof(buf
)) {
102 fprintf(stderr
, "Too small buffer (%zu B) to read all meta info (%zd B)\n", sizeof(buf
), metasize
);
109 printf("Entity offset:\t%ld\n", ftell(seama
) - sizeof(hdr
));
110 printf("Entity size:\t%zd\n", sizeof(hdr
) + metasize
+ imagesize
);
111 printf("Meta size:\t%zd\n", metasize
);
112 printf("Image size:\t%zd\n", imagesize
);
114 bytes
= fread(buf
, 1, metasize
, seama
);
115 if (bytes
!= metasize
) {
116 fprintf(stderr
, "Couldn't read %zd B of meta\n", metasize
);
121 end
= (char *)&buf
[metasize
- 1];
123 for (tmp
= (char *)buf
; tmp
< end
&& strlen(tmp
); tmp
+= strlen(tmp
) + 1) {
124 printf("Meta entry:\t%s\n", tmp
);
127 fseek(seama
, imagesize
, SEEK_CUR
);
135 static int oseama_info(int argc
, char **argv
) {
137 struct seama_seal_header hdr
;
145 fprintf(stderr
, "No Seama file passed\n");
149 seama_path
= argv
[2];
152 oseama_info_parse_options(argc
, argv
);
154 seama
= fopen(seama_path
, "r");
156 fprintf(stderr
, "Couldn't open %s\n", seama_path
);
161 bytes
= fread(&hdr
, 1, sizeof(hdr
), seama
);
162 if (bytes
!= sizeof(hdr
)) {
163 fprintf(stderr
, "Couldn't read %s header\n", seama_path
);
167 metasize
= be16_to_cpu(hdr
.metasize
);
168 imagesize
= be32_to_cpu(hdr
.imagesize
);
170 if (be32_to_cpu(hdr
.magic
) != SEAMA_MAGIC
) {
171 fprintf(stderr
, "Invalid Seama magic: 0x%08x\n", be32_to_cpu(hdr
.magic
));
176 if (metasize
>= sizeof(buf
)) {
177 fprintf(stderr
, "Too small buffer (%zu B) to read all meta info (%d B)\n", sizeof(buf
), metasize
);
183 fprintf(stderr
, "Invalid Seama image size: 0x%08x (should be 0)\n", imagesize
);
188 bytes
= fread(buf
, 1, metasize
, seama
);
189 if (bytes
!= metasize
) {
190 fprintf(stderr
, "Couldn't read %d B of meta\n", metasize
);
195 if (entity_idx
< 0) {
198 printf("Meta size:\t%d\n", metasize
);
199 printf("Image size:\t%d\n", imagesize
);
201 end
= (char *)&buf
[metasize
- 1];
203 for (tmp
= (char *)buf
; tmp
< end
&& strlen(tmp
); tmp
+= strlen(tmp
) + 1) {
204 printf("Meta entry:\t%s\n", tmp
);
208 oseama_info_entities(seama
);
216 /**************************************************
218 **************************************************/
220 static ssize_t
oseama_entity_append_file(FILE *seama
, const char *in_path
) {
226 in
= fopen(in_path
, "r");
228 fprintf(stderr
, "Couldn't open %s\n", in_path
);
232 while ((bytes
= fread(buf
, 1, sizeof(buf
), in
)) > 0) {
233 if (fwrite(buf
, 1, bytes
, seama
) != bytes
) {
234 fprintf(stderr
, "Couldn't write %zu B to %s\n", bytes
, seama_path
);
246 static ssize_t
oseama_entity_append_zeros(FILE *seama
, size_t length
) {
249 buf
= malloc(length
);
252 memset(buf
, 0, length
);
254 if (fwrite(buf
, 1, length
, seama
) != length
) {
255 fprintf(stderr
, "Couldn't write %zu B to %s\n", length
, seama_path
);
262 static ssize_t
oseama_entity_align(FILE *seama
, size_t curr_offset
, size_t alignment
) {
263 if (curr_offset
& (alignment
- 1)) {
264 size_t length
= alignment
- (curr_offset
% alignment
);
266 return oseama_entity_append_zeros(seama
, length
);
272 static int oseama_entity_write_hdr(FILE *seama
, size_t metasize
, size_t imagesize
) {
273 struct seama_entity_header hdr
= {};
275 size_t length
= imagesize
;
279 fseek(seama
, sizeof(hdr
) + metasize
, SEEK_SET
);
281 while ((bytes
= fread(buf
, 1, oseama_min(sizeof(buf
), length
), seama
)) > 0) {
282 MD5_Update(&ctx
, buf
, bytes
);
285 MD5_Final(hdr
.md5
, &ctx
);
287 hdr
.magic
= cpu_to_be32(SEAMA_MAGIC
);
288 hdr
.metasize
= cpu_to_be16(metasize
);
289 hdr
.imagesize
= cpu_to_be32(imagesize
);
291 fseek(seama
, 0, SEEK_SET
);
292 bytes
= fwrite(&hdr
, 1, sizeof(hdr
), seama
);
293 if (bytes
!= sizeof(hdr
)) {
294 fprintf(stderr
, "Couldn't write Seama entity header to %s\n", seama_path
);
301 static int oseama_entity(int argc
, char **argv
) {
304 size_t curr_offset
= sizeof(struct seama_entity_header
);
305 size_t metasize
= 0, imagesize
= 0;
310 fprintf(stderr
, "No Seama file passed\n");
314 seama_path
= argv
[2];
316 seama
= fopen(seama_path
, "w+");
318 fprintf(stderr
, "Couldn't open %s\n", seama_path
);
322 fseek(seama
, curr_offset
, SEEK_SET
);
325 while ((c
= getopt(argc
, argv
, "m:f:b:")) != -1) {
328 sbytes
= fwrite(optarg
, 1, strlen(optarg
) + 1, seama
);
330 fprintf(stderr
, "Failed to write meta %s\n", optarg
);
332 curr_offset
+= sbytes
;
336 sbytes
= oseama_entity_align(seama
, curr_offset
, 4);
338 fprintf(stderr
, "Failed to append zeros\n");
340 curr_offset
+= sbytes
;
352 while ((c
= getopt(argc
, argv
, "m:f:b:")) != -1) {
357 sbytes
= oseama_entity_append_file(seama
, optarg
);
359 fprintf(stderr
, "Failed to append file %s\n", optarg
);
361 curr_offset
+= sbytes
;
366 sbytes
= strtol(optarg
, NULL
, 0) - curr_offset
;
368 fprintf(stderr
, "Current Seama entity length is 0x%zx, can't pad it with zeros to 0x%lx\n", curr_offset
, strtol(optarg
, NULL
, 0));
370 sbytes
= oseama_entity_append_zeros(seama
, sbytes
);
372 fprintf(stderr
, "Failed to append zeros\n");
374 curr_offset
+= sbytes
;
384 oseama_entity_write_hdr(seama
, metasize
, imagesize
);
391 /**************************************************
393 **************************************************/
395 static void oseama_extract_parse_options(int argc
, char **argv
) {
398 while ((c
= getopt(argc
, argv
, "e:o:")) != -1) {
401 entity_idx
= atoi(optarg
);
410 static int oseama_extract_entity(FILE *seama
, FILE *out
) {
411 struct seama_entity_header hdr
;
412 size_t bytes
, metasize
, imagesize
, length
;
417 while ((bytes
= fread(&hdr
, 1, sizeof(hdr
), seama
)) == sizeof(hdr
)) {
418 if (be32_to_cpu(hdr
.magic
) != SEAMA_MAGIC
) {
419 fprintf(stderr
, "Invalid Seama magic: 0x%08x\n", be32_to_cpu(hdr
.magic
));
423 metasize
= be16_to_cpu(hdr
.metasize
);
424 imagesize
= be32_to_cpu(hdr
.imagesize
);
426 if (i
!= entity_idx
) {
427 fseek(seama
, metasize
+ imagesize
, SEEK_CUR
);
432 fseek(seama
, -sizeof(hdr
), SEEK_CUR
);
434 length
= sizeof(hdr
) + metasize
+ imagesize
;
435 while ((bytes
= fread(buf
, 1, oseama_min(sizeof(buf
), length
), seama
)) > 0) {
436 if (fwrite(buf
, 1, bytes
, out
) != bytes
) {
437 fprintf(stderr
, "Couldn't write %zu B to %s\n", bytes
, out_path
);
445 fprintf(stderr
, "Couldn't extract whole entity %d from %s (%zu B left)\n", entity_idx
, seama_path
, length
);
456 static int oseama_extract(int argc
, char **argv
) {
459 struct seama_seal_header hdr
;
465 fprintf(stderr
, "No Seama file passed\n");
469 seama_path
= argv
[2];
472 oseama_extract_parse_options(argc
, argv
);
473 if (entity_idx
< 0) {
474 fprintf(stderr
, "No entity specified\n");
477 } else if (!out_path
) {
478 fprintf(stderr
, "No output file specified\n");
483 seama
= fopen(seama_path
, "r");
485 fprintf(stderr
, "Couldn't open %s\n", seama_path
);
490 out
= fopen(out_path
, "w");
492 fprintf(stderr
, "Couldn't open %s\n", out_path
);
494 goto err_close_seama
;
497 bytes
= fread(&hdr
, 1, sizeof(hdr
), seama
);
498 if (bytes
!= sizeof(hdr
)) {
499 fprintf(stderr
, "Couldn't read %s header\n", seama_path
);
503 metasize
= be16_to_cpu(hdr
.metasize
);
505 fseek(seama
, metasize
, SEEK_CUR
);
507 oseama_extract_entity(seama
, out
);
517 /**************************************************
519 **************************************************/
521 static void usage() {
524 printf("Info about Seama seal (container):\n");
525 printf("\toseama info <file> [options]\n");
526 printf("\t-e\t\t\t\tprint info about specified entity only\n");
528 printf("Create Seama entity:\n");
529 printf("\toseama entity <file> [options]\n");
530 printf("\t-m meta\t\t\t\tmeta into to put in header\n");
531 printf("\t-f file\t\t\t\tappend content from file\n");
532 printf("\t-b offset\t\t\tappend zeros till reaching absolute offset\n");
534 printf("Extract from Seama seal (container):\n");
535 printf("\toseama extract <file> [options]\n");
536 printf("\t-e\t\t\t\tindex of entity to extract\n");
537 printf("\t-o file\t\t\t\toutput file\n");
540 int main(int argc
, char **argv
) {
542 if (!strcmp(argv
[1], "info"))
543 return oseama_info(argc
, argv
);
544 else if (!strcmp(argv
[1], "entity"))
545 return oseama_entity(argc
, argv
);
546 else if (!strcmp(argv
[1], "extract"))
547 return oseama_extract(argc
, argv
);