]> git.sesse.net Git - pistorm/blob - platforms/amiga/piscsi/device_driver_amiga/makerom.c
e63c777c796de7f088f4e503efac892b5598b69f
[pistorm] / platforms / amiga / piscsi / device_driver_amiga / makerom.c
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <string.h>
4 #include <stdlib.h>
5
6 #define BOOTLDR_SIZE 0x1000
7 #define DIAG_TOTAL_SIZE 0x4000
8
9 char *rombuf, *zerobuf, *devicebuf;
10
11 int main(int argc, char *argv[]) {
12     FILE *rom = fopen("bootrom", "rb");
13     if (!rom) {
14         printf("Could not open file bootrom for reading.\n");
15         return 1;
16     }
17     FILE *out = fopen("../piscsi.rom", "wb+");
18     if (!out) {
19         printf("Could not open file piscsi.rom for writing.\n");
20         fclose(rom);
21         return 1;
22     }
23     FILE *device = NULL;
24     if (argc > 1) {
25         device = fopen(argv[1], "rb");
26     }
27     else {
28         device = fopen("pi-scsi.device", "rb");
29     }
30     if (!device) {
31         printf("Could not open device file for reading.\n");
32         fclose(rom);
33         fclose(out);
34         return 1;
35     }
36
37     fseek(device, 0, SEEK_END);
38     fseek(rom, 0, SEEK_END);
39     uint32_t rom_size = ftell(rom);
40     uint32_t device_size = ftell(device);
41     fseek(rom, 0, SEEK_SET);
42     fseek(device, 0, SEEK_SET);
43
44     uint32_t pad_size = BOOTLDR_SIZE - rom_size;
45
46     rombuf = malloc(rom_size);
47     devicebuf = malloc(device_size);
48     zerobuf = malloc(pad_size);
49     memset(zerobuf, 0x00, pad_size);
50
51     fread(rombuf, rom_size, 1, rom);
52     fread(devicebuf, device_size, 1, device);
53
54     fwrite(rombuf, rom_size, 1, out);
55     fwrite(zerobuf, pad_size, 1, out);
56     fwrite(devicebuf, device_size, 1, out);
57
58     free(zerobuf);
59     zerobuf = malloc(DIAG_TOTAL_SIZE - (rom_size + pad_size + device_size));
60     memset(zerobuf, 0x00, DIAG_TOTAL_SIZE - (rom_size + pad_size + device_size));
61     fwrite(zerobuf, DIAG_TOTAL_SIZE - (rom_size + pad_size + device_size), 1, out);
62
63     printf("piscsi.rom successfully created.\n");
64
65     free(rombuf);
66     free(zerobuf);
67     free(devicebuf);
68     
69     fclose(out);
70     fclose(device);
71     fclose(rom);
72
73     return 0;
74 }