Rev 7869 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 7869 | Rev 7882 | ||
---|---|---|---|
Line 7... | Line 7... | ||
7 | If output folder is skipped, the image will be unpacked at /TMP0/1/[file-name] |
7 | If output folder is skipped, the image will be unpacked at /TMP0/1/[file-name] |
Line 8... | Line 8... | ||
8 | 8 | ||
9 | Author: Magomed Kostoev (Boppan, mkostoevr): FAT12 file system, driver. |
9 | Author: Magomed Kostoev (Boppan, mkostoevr): FAT12 file system, driver. |
Line -... | Line 10... | ||
- | 10 | Contributor: Kiril Lipatov (Leency) */ |
|
- | 11 | ||
- | 12 | #ifdef __TINYC__ |
|
- | 13 | # define TCC 1 |
|
- | 14 | #else |
|
- | 15 | # define GCC 1 |
|
10 | Contributor: Kiril Lipatov (Leency) */ |
16 | #endif |
11 | 17 | ||
12 | #include |
18 | #include |
13 | #include |
19 | #include |
14 | #include |
20 | #include |
Line 15... | Line 21... | ||
15 | #include |
21 | #include |
16 | #include |
22 | #include |
- | 23 | ||
17 | 24 | #if TCC |
|
18 | #define TCC 0 |
25 | # include |
- | 26 | # define printf con_printf |
|
19 | #define GCC 1 |
27 | # define puts con_write_asciiz |
20 | #include "compiller.h" |
28 | #else |
21 | 29 | # define con_init_console_dll() 0 |
|
Line 22... | Line 30... | ||
22 | #ifdef GCC |
30 | # define con_set_title(s) |
23 | #define con_init con_init |
31 | # define con_exit(close) |
24 | #endif |
32 | #endif |
Line 66... | Line 74... | ||
66 | static int fat12__forEachFile(const Fat12 *this, ForEachCallback callback, void *callbackParam); |
74 | static int fat12__forEachFile(const Fat12 *this, ForEachCallback callback, void *callbackParam); |
67 | static int fat12__open(Fat12 *this, const char *img); |
75 | static int fat12__open(Fat12 *this, const char *img); |
68 | static int fat12__error(Fat12 *this, const char *errorMessage); |
76 | static int fat12__error(Fat12 *this, const char *errorMessage); |
Line 69... | Line 77... | ||
69 | 77 | ||
70 | static void mkdir(const char *name) { |
- | |
71 | #ifdef TCC |
78 | static void mkdir(const char *name) { |
72 | struct { |
79 | struct { |
73 | int fn; |
80 | int fn; |
74 | int unused[4]; |
81 | int unused[4]; |
75 | char b; |
82 | char b; |
76 | const char *path __attribute__((packed)); |
83 | const char *path __attribute__((packed)); |
77 | } info; |
84 | } info; |
78 | #else |
- | |
79 | struct { |
- | |
80 | int fn; |
- | |
81 | int unused[4]; |
- | |
82 | char b; |
- | |
83 | const char *path; |
- | |
84 | } __attribute__((packed)) info; |
- | |
85 | #endif |
85 | |
86 | memset(&info, 0, sizeof(info)); |
86 | memset(&info, 0, sizeof(info)); |
87 | info.fn = 9; |
87 | info.fn = 9; |
88 | info.b = 0; |
88 | info.b = 0; |
89 | info.path = name; |
89 | info.path = name; |
Line 374... | Line 374... | ||
374 | return 0; |
374 | return 0; |
375 | } |
375 | } |
Line 376... | Line 376... | ||
376 | 376 | ||
377 | static int handleError(const Fat12 *fat12) { |
377 | static int handleError(const Fat12 *fat12) { |
- | 378 | printf("Error in Fat12: %s\n", fat12->errorMessage); |
|
378 | printf("Error in Fat12: %s\n", fat12->errorMessage); |
379 | con_exit(0); |
379 | return -1; |
380 | return -1; |
Line 380... | Line 381... | ||
380 | } |
381 | } |
381 | - | ||
382 | void writeFile(const char *fileName, int size, const uint8_t *data) { |
- | |
383 | // FILE *fp = NULL; |
- | |
384 | // if (!(fp = fopen(fileName, "wb"))) { perror(NULL); } |
382 | |
385 | // fwrite(data, 1, size, fp); |
383 | void writeFile(const char *fileName, int size, const uint8_t *data) { |
386 | // fclose(fp); |
384 | #if TCC |
387 | struct Info { |
385 | struct Info { |
388 | int number; |
386 | int number; |
389 | int reserved0; |
387 | int reserved0; |
390 | int reserved1; |
388 | int reserved1; |
391 | int dataSize; |
389 | int dataSize; |
392 | const void *data; |
390 | const void *data; |
393 | char zero; |
391 | char zero; |
Line -... | Line 392... | ||
- | 392 | const char *name __attribute__((packed)); |
|
394 | const char *name; |
393 | } info; |
395 | } __attribute__((packed)) *info = calloc(sizeof(struct Info), 1); |
394 | |
396 | 395 | memset(&info, 0, sizeof(struct Info)); |
|
397 | info->number = 2; // create/overwrite file |
396 | info.number = 2; // create/overwrite file |
398 | info->dataSize = size; |
397 | info.dataSize = size; |
399 | info->data = data; |
398 | info.data = data; |
- | 399 | info.zero = 0; |
|
- | 400 | info.name = fileName; |
|
- | 401 | asm volatile ("int $0x40" :: "a"(70), "b"(&info)); |
|
- | 402 | #else |
|
- | 403 | FILE *fp = NULL; |
|
- | 404 | if (!(fp = fopen(fileName, "wb"))) { perror(NULL); } |
|
400 | info->zero = 0; |
405 | fwrite(data, 1, size, fp); |
Line 401... | Line 406... | ||
401 | info->name = fileName; |
406 | fclose(fp); |
402 | asm volatile ("int $0x40" :: "a"(70), "b"(info)); |
407 | #endif |
Line 420... | Line 425... | ||
420 | mkdir_p(outputPath->data); |
425 | mkdir_p(outputPath->data); |
421 | *fileNameDelim = '/'; |
426 | *fileNameDelim = '/'; |
422 | } |
427 | } |
423 | } |
428 | } |
424 | printf("Extracting %s\n", outputPath->data); |
429 | printf("Extracting %s\n", outputPath->data); |
425 | #ifdef TCC |
- | |
426 | FILE *fp = NULL; |
- | |
427 | if (!(fp = fopen(outputPath->data, "wb"))) { perror(NULL); } |
- | |
428 | fwrite(data, 1, size, fp); |
- | |
429 | fclose(fp); |
- | |
430 | #else |
- | |
431 | writeFile(outputPath->data, size, data); |
430 | writeFile(outputPath->data, size, data); |
432 | #endif |
- | |
433 | outputPath->data[outputPath->length] = '\0'; |
431 | outputPath->data[outputPath->length] = '\0'; |
434 | return 0; |
432 | return 0; |
435 | } |
433 | } |
Line 436... | Line 434... | ||
436 | 434 | ||
437 | 435 | ||
438 | 436 | ||
439 | int main(int argc, char* argv[]) { |
437 | int main(int argc, char* argv[]) { |
440 | Fat12 fat12 = { 0 }; |
438 | Fat12 fat12 = { 0 }; |
Line 441... | Line 439... | ||
441 | char *imageFile = NULL; |
439 | char *imageFile = NULL; |
442 | String outputFolder = { 0 }; |
440 | String outputFolder = { 0 }; |
Line 443... | Line 441... | ||
443 | int exit_code = 0; |
441 | int closeOnExit = 0; |
444 | 442 | ||
445 | char app_title[] = "UnImg - kolibri.img file unpacker"; |
443 | if (con_init_console_dll()) { return -1; } |
446 | con_init(-1, -1, -1, 350, app_title); |
444 | con_set_title("UnImg - kolibri.img file unpacker"); |
447 | 445 | ||
448 | if (argc < 2) { |
446 | if (argc < 2) { |
449 | puts(" Usage:"); |
447 | puts(" Usage:"); |
450 | puts(" unimg \"/path/to/kolibri.img\" \"/optional/extract/path\""); |
448 | puts(" unimg \"/path/to/kolibri.img\" \"/optional/extract/path\""); |
451 | puts(" where optional key [-e] is exit on success"); |
449 | puts(" where optional key [-e] is exit on success"); |
452 | exit(exit_code); |
450 | con_exit(0); |
Line 453... | Line 451... | ||
453 | return -1; |
451 | return -1; |
454 | } else { |
452 | } else { |
Line 455... | Line 453... | ||
455 | imageFile = argv[1]; |
453 | imageFile = argv[1]; |
456 | printf("File: %s\n", imageFile); |
454 | printf("File: %s\n", imageFile); |
457 | } |
455 | } |
458 | 456 | ||
459 | outputFolder.capacity = 4096; |
457 | outputFolder.capacity = 4096; |
460 | outputFolder.data = malloc(outputFolder.capacity); |
458 | outputFolder.data = malloc(outputFolder.capacity); |
Line 461... | Line 459... | ||
461 | 459 | ||
Line 462... | Line 460... | ||
462 | //! ACHTUNG: possible buffer overflow, is 4096 enough in KolibriOS? |
460 | //! ACHTUNG: possible buffer overflow, is 4096 enough in KolibriOS? |
463 | if (argc >= 3 && argv[2][0] != '-') strcpy(outputFolder.data, argv[2]); |
461 | if (argc >= 3 && argv[2][0] != '-') { strcpy(outputFolder.data, argv[2]); } |
Line 464... | Line 462... | ||
464 | else { |
462 | else { |
465 | strcpy(outputFolder.data, "/tmp0/1"); |
463 | strcpy(outputFolder.data, "/tmp0/1"); |
466 | strcat(outputFolder.data, strrchr(imageFile, '/')); |
464 | strcat(outputFolder.data, strrchr(imageFile, '/')); |
467 | } |
- | |
468 | 465 | } |
|
469 | outputFolder.length = strlen(outputFolder.data); |
466 | |
470 | 467 | outputFolder.length = strlen(outputFolder.data); |
|
Line 471... | Line 468... | ||
471 | // handle -e parameter - exit on success |
468 | |
472 | if (argc >= 3 && !strcmp(argv[argc - 1], "-e")) { exit_code = 1; } |
469 | // handle -e parameter - exit on success |
- | 470 | if (argc >= 3 && !strcmp(argv[argc - 1], "-e")) { closeOnExit = 1; } |
|
473 | 471 |