2020-06-30 22:02:55 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
2020-09-04 06:21:58 +03:00
|
|
|
extern char _binary_src_limine_bin_start[];
|
2020-06-30 22:02:55 +03:00
|
|
|
|
2020-09-04 06:21:58 +03:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
if (argc < 2) {
|
|
|
|
printf("Usage: %s <device> [stage2 start sector]\n", argv[0]);
|
2020-06-30 22:02:55 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-09-04 06:21:58 +03:00
|
|
|
FILE *device = fopen(argv[1], "r+b");
|
2020-06-30 22:02:55 +03:00
|
|
|
if (device == NULL) {
|
|
|
|
perror("Error: ");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t stage2_sect = 1;
|
2020-09-04 06:21:58 +03:00
|
|
|
if (argc >= 3)
|
|
|
|
sscanf(argv[2], "%" SCNu32, &stage2_sect);
|
2020-06-30 22:02:55 +03:00
|
|
|
|
2020-09-04 06:21:58 +03:00
|
|
|
// Save the original partition table of the device
|
2020-06-30 22:02:55 +03:00
|
|
|
char orig_mbr[64];
|
|
|
|
fseek(device, 446, SEEK_SET);
|
|
|
|
fread(orig_mbr, 1, 64, device);
|
|
|
|
|
2020-09-04 06:21:58 +03:00
|
|
|
// Write the bootsector from the bootloader to the device
|
2020-06-30 22:02:55 +03:00
|
|
|
fseek(device, 0, SEEK_SET);
|
2020-09-04 06:21:58 +03:00
|
|
|
fwrite(&_binary_src_limine_bin_start[0], 1, 512, device);
|
2020-06-30 22:02:55 +03:00
|
|
|
|
2020-09-04 06:21:58 +03:00
|
|
|
// Write the rest of stage 2 to the device
|
2020-06-30 22:02:55 +03:00
|
|
|
fseek(device, stage2_sect * 512, SEEK_SET);
|
2020-09-04 06:21:58 +03:00
|
|
|
fwrite(&_binary_src_limine_bin_start[512], 63, 512, device);
|
2020-06-30 22:02:55 +03:00
|
|
|
|
2020-09-04 06:21:58 +03:00
|
|
|
// Hardcode in the bootsector the location of stage 2
|
2020-06-30 22:02:55 +03:00
|
|
|
fseek(device, 0x1b0, SEEK_SET);
|
2020-09-04 06:21:58 +03:00
|
|
|
fwrite(&stage2_sect, 1, sizeof(uint32_t), device);
|
2020-06-30 22:02:55 +03:00
|
|
|
|
2020-09-04 06:21:58 +03:00
|
|
|
// Write back the saved partition table to the device
|
2020-06-30 22:02:55 +03:00
|
|
|
fseek(device, 446, SEEK_SET);
|
|
|
|
fwrite(orig_mbr, 1, 64, device);
|
|
|
|
|
|
|
|
fclose(device);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|