rulimine/limine-enroll-config.c

144 lines
3.6 KiB
C
Raw Permalink Normal View History

2023-02-09 01:47:55 +03:00
#undef IS_WINDOWS
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#define IS_WINDOWS 1
#endif
2023-02-07 03:04:05 +03:00
#include <stddef.h>
2023-02-09 01:47:55 +03:00
#include <stdbool.h>
2023-02-07 03:04:05 +03:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CONFIG_B2SUM_SIGNATURE "++CONFIG_B2SUM_SIGNATURE++"
2023-02-09 01:47:55 +03:00
static void usage(const char *name) {
printf("Usage: %s <Limine executable> <BLAKE2B of config file>\n", name);
printf("\n");
2023-07-27 01:35:50 +03:00
printf(" --reset Remove enrolled BLAKE2B, will not check config integrity\n");
2023-02-09 01:47:55 +03:00
printf("\n");
printf(" --quiet Do not print verbose diagnostic messages\n");
printf("\n");
printf(" --help | -h Display this help message\n");
printf("\n");
}
static void remove_arg(int *argc, char *argv[], int index) {
for (int i = index; i < *argc - 1; i++) {
argv[i] = argv[i + 1];
}
(*argc)--;
argv[*argc] = NULL;
}
2023-02-07 03:04:05 +03:00
int main(int argc, char *argv[]) {
2023-02-09 01:47:55 +03:00
int ret = EXIT_FAILURE;
2023-02-07 03:04:05 +03:00
char *bootloader = NULL;
FILE *bootloader_file = NULL;
2023-02-09 01:47:55 +03:00
bool quiet = false;
bool reset = false;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
usage(argv[0]);
return EXIT_SUCCESS;
} else if (strcmp(argv[i], "--quiet") == 0) {
remove_arg(&argc, argv, i);
quiet = true;
} else if (strcmp(argv[i], "--reset") == 0) {
remove_arg(&argc, argv, i);
reset = true;
}
}
2023-02-07 03:04:05 +03:00
2023-02-09 01:47:55 +03:00
if (argc <= (reset ? 1 : 2)) {
usage(argv[0]);
#ifdef IS_WINDOWS
system("pause");
#endif
return EXIT_FAILURE;
2023-02-07 03:04:05 +03:00
}
2023-02-09 01:47:55 +03:00
if (!reset && strlen(argv[2]) != 128) {
2023-02-07 03:04:05 +03:00
fprintf(stderr, "ERROR: BLAKE2B specified is not 128 characters long\n");
goto cleanup;
}
bootloader_file = fopen(argv[1], "r+b");
if (bootloader_file == NULL) {
perror("ERROR");
goto cleanup;;
}
if (fseek(bootloader_file, 0, SEEK_END) != 0) {
perror("ERROR");
goto cleanup;
}
size_t bootloader_size = ftell(bootloader_file);
rewind(bootloader_file);
bootloader = malloc(bootloader_size);
if (bootloader == NULL) {
perror("ERROR");
goto cleanup;
}
if (fread(bootloader, bootloader_size, 1, bootloader_file) != 1) {
perror("ERROR");
goto cleanup;
}
char *checksum_loc = NULL;
size_t checked_count = 0;
const char *config_b2sum_sign = CONFIG_B2SUM_SIGNATURE;
2023-02-09 01:47:55 +03:00
for (size_t i = 0; i < bootloader_size - ((sizeof(CONFIG_B2SUM_SIGNATURE) - 1) + 128) + 1; i++) {
2023-02-07 03:04:05 +03:00
if (bootloader[i] != config_b2sum_sign[checked_count]) {
checked_count = 0;
continue;
}
checked_count++;
if (checked_count == sizeof(CONFIG_B2SUM_SIGNATURE) - 1) {
checksum_loc = &bootloader[i + 1];
break;
}
}
if (checksum_loc == NULL) {
fprintf(stderr, "ERROR: Checksum location not found in provided executable\n");
goto cleanup;
}
2023-02-09 01:47:55 +03:00
if (!reset) {
memcpy(checksum_loc, argv[2], 128);
} else {
memset(checksum_loc, '0', 128);
}
2023-02-07 03:04:05 +03:00
if (fseek(bootloader_file, 0, SEEK_SET) != 0) {
perror("ERROR");
goto cleanup;
}
if (fwrite(bootloader, bootloader_size, 1, bootloader_file) != 1) {
perror("ERROR");
goto cleanup;
}
2023-02-09 01:47:55 +03:00
if (!quiet) {
fprintf(stderr, "Config file BLAKE2B successfully %s!\n", reset ? "reset" : "enrolled");
}
ret = EXIT_SUCCESS;
2023-02-07 03:04:05 +03:00
cleanup:
if (bootloader != NULL) {
free(bootloader);
}
if (bootloader_file != NULL) {
fclose(bootloader_file);
}
return ret;
}