Make it buildable by both TCC and GCC.

Remove extra readme.txt (all info is in fat12.c)

git-svn-id: svn://kolibrios.org@7882 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Magomed Kostoev (mkostoevr) 2020-05-07 09:30:37 +00:00
parent 40267e753c
commit dd92e74f77
4 changed files with 48 additions and 55 deletions

View File

@ -9,18 +9,26 @@ Usage: unimg path/to/img [output/folder] [-e]
Author: Magomed Kostoev (Boppan, mkostoevr): FAT12 file system, driver. Author: Magomed Kostoev (Boppan, mkostoevr): FAT12 file system, driver.
Contributor: Kiril Lipatov (Leency) */ Contributor: Kiril Lipatov (Leency) */
#ifdef __TINYC__
# define TCC 1
#else
# define GCC 1
#endif
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define TCC 0 #if TCC
#define GCC 1 # include <conio.h>
#include "compiller.h" # define printf con_printf
# define puts con_write_asciiz
#ifdef GCC #else
#define con_init con_init # define con_init_console_dll() 0
# define con_set_title(s)
# define con_exit(close)
#endif #endif
typedef struct { typedef struct {
@ -68,21 +76,13 @@ static int fat12__open(Fat12 *this, const char *img);
static int fat12__error(Fat12 *this, const char *errorMessage); static int fat12__error(Fat12 *this, const char *errorMessage);
static void mkdir(const char *name) { static void mkdir(const char *name) {
#ifdef TCC
struct { struct {
int fn; int fn;
int unused[4]; int unused[4];
char b; char b;
const char *path __attribute__((packed)); const char *path __attribute__((packed));
} info; } info;
#else
struct {
int fn;
int unused[4];
char b;
const char *path;
} __attribute__((packed)) info;
#endif
memset(&info, 0, sizeof(info)); memset(&info, 0, sizeof(info));
info.fn = 9; info.fn = 9;
info.b = 0; info.b = 0;
@ -376,14 +376,12 @@ static int fat12__error(Fat12 *this, const char *errorMessage) {
static int handleError(const Fat12 *fat12) { static int handleError(const Fat12 *fat12) {
printf("Error in Fat12: %s\n", fat12->errorMessage); printf("Error in Fat12: %s\n", fat12->errorMessage);
con_exit(0);
return -1; return -1;
} }
void writeFile(const char *fileName, int size, const uint8_t *data) { void writeFile(const char *fileName, int size, const uint8_t *data) {
// FILE *fp = NULL; #if TCC
// if (!(fp = fopen(fileName, "wb"))) { perror(NULL); }
// fwrite(data, 1, size, fp);
// fclose(fp);
struct Info { struct Info {
int number; int number;
int reserved0; int reserved0;
@ -391,15 +389,22 @@ void writeFile(const char *fileName, int size, const uint8_t *data) {
int dataSize; int dataSize;
const void *data; const void *data;
char zero; char zero;
const char *name; const char *name __attribute__((packed));
} __attribute__((packed)) *info = calloc(sizeof(struct Info), 1); } info;
info->number = 2; // create/overwrite file memset(&info, 0, sizeof(struct Info));
info->dataSize = size; info.number = 2; // create/overwrite file
info->data = data; info.dataSize = size;
info->zero = 0; info.data = data;
info->name = fileName; info.zero = 0;
asm volatile ("int $0x40" :: "a"(70), "b"(info)); info.name = fileName;
asm volatile ("int $0x40" :: "a"(70), "b"(&info));
#else
FILE *fp = NULL;
if (!(fp = fopen(fileName, "wb"))) { perror(NULL); }
fwrite(data, 1, size, fp);
fclose(fp);
#endif
} }
static int callback(const char *name, size_t size, const uint8_t *data, void *param) { static int callback(const char *name, size_t size, const uint8_t *data, void *param) {
@ -422,14 +427,7 @@ static int callback(const char *name, size_t size, const uint8_t *data, void *pa
} }
} }
printf("Extracting %s\n", outputPath->data); printf("Extracting %s\n", outputPath->data);
#ifdef TCC writeFile(outputPath->data, size, data);
FILE *fp = NULL;
if (!(fp = fopen(outputPath->data, "wb"))) { perror(NULL); }
fwrite(data, 1, size, fp);
fclose(fp);
#else
writeFile(outputPath->data, size, data);
#endif
outputPath->data[outputPath->length] = '\0'; outputPath->data[outputPath->length] = '\0';
return 0; return 0;
} }
@ -440,45 +438,45 @@ int main(int argc, char* argv[]) {
Fat12 fat12 = { 0 }; Fat12 fat12 = { 0 };
char *imageFile = NULL; char *imageFile = NULL;
String outputFolder = { 0 }; String outputFolder = { 0 };
int exit_code = 0; int closeOnExit = 0;
char app_title[] = "UnImg - kolibri.img file unpacker"; if (con_init_console_dll()) { return -1; }
con_init(-1, -1, -1, 350, app_title); con_set_title("UnImg - kolibri.img file unpacker");
if (argc < 2) { if (argc < 2) {
puts(" Usage:"); puts(" Usage:");
puts(" unimg \"/path/to/kolibri.img\" \"/optional/extract/path\""); puts(" unimg \"/path/to/kolibri.img\" \"/optional/extract/path\"");
puts(" where optional key [-e] is exit on success"); puts(" where optional key [-e] is exit on success");
exit(exit_code); con_exit(0);
return -1; return -1;
} else { } else {
imageFile = argv[1]; imageFile = argv[1];
printf("File: %s\n", imageFile); printf("File: %s\n", imageFile);
} }
outputFolder.capacity = 4096; outputFolder.capacity = 4096;
outputFolder.data = malloc(outputFolder.capacity); outputFolder.data = malloc(outputFolder.capacity);
//! ACHTUNG: possible buffer overflow, is 4096 enough in KolibriOS? //! ACHTUNG: possible buffer overflow, is 4096 enough in KolibriOS?
if (argc >= 3 && argv[2][0] != '-') strcpy(outputFolder.data, argv[2]); if (argc >= 3 && argv[2][0] != '-') { strcpy(outputFolder.data, argv[2]); }
else { else {
strcpy(outputFolder.data, "/tmp0/1"); strcpy(outputFolder.data, "/tmp0/1");
strcat(outputFolder.data, strrchr(imageFile, '/')); strcat(outputFolder.data, strrchr(imageFile, '/'));
} }
outputFolder.length = strlen(outputFolder.data); outputFolder.length = strlen(outputFolder.data);
// handle -e parameter - exit on success // handle -e parameter - exit on success
if (argc >= 3 && !strcmp(argv[argc - 1], "-e")) { exit_code = 1; } if (argc >= 3 && !strcmp(argv[argc - 1], "-e")) { closeOnExit = 1; }
if (!fat12__open(&fat12, imageFile)) { if (!fat12__open(&fat12, imageFile)) {
return handleError(&fat12); return handleError(&fat12);
} }
if (!fat12__forEachFile(&fat12, callback, &outputFolder)) { if (!fat12__forEachFile(&fat12, callback, &outputFolder)) {
return handleError(&fat12); return handleError(&fat12);
} }
puts("\nDONE!"); puts("\nDONE!");
exit(exit_code); con_exit(closeOnExit);
return 0;
} }

View File

@ -1,7 +1,4 @@
@echo #define COMPILLER TCC > compiller.h
gcc -m32 -c -fomit-frame-pointer -IC:\Users\Boppan\Documents\KolibriOS\contrib/sdk/sources/newlib/libc/include fat12.c -o unimg.o -Wall -Wextra gcc -m32 -c -fomit-frame-pointer -IC:\Users\Boppan\Documents\KolibriOS\contrib/sdk/sources/newlib/libc/include fat12.c -o unimg.o -Wall -Wextra
kos32-ld unimg.o -o unimg -static -S -nostdlib -T C:\Users\Boppan\Documents\KolibriOS\contrib/sdk/sources/newlib/app.lds --image-base 0 -L "C:\Program Files (x86)\kos32-msys-5.4.0\win32\lib" -lgcc -lapp -lc.dll kos32-ld unimg.o -o unimg -static -S -nostdlib -T C:\Users\Boppan\Documents\KolibriOS\contrib/sdk/sources/newlib/app.lds --image-base 0 -L "C:\Program Files (x86)\kos32-msys-5.4.0\win32\lib" -lgcc -lapp -lc.dll
kos32-objcopy unimg -O binary kos32-objcopy unimg -O binary
@del compiller.h
@pause @pause

View File

@ -1,4 +1,2 @@
@echo #define COMPILLER TCC > compiller.h
kos32-tcc fat12.c -lck -o unimg kos32-tcc fat12.c -lck -o unimg
@del compiller.h
@pause @pause

Binary file not shown.