limine-install: Make it pure C11

This commit is contained in:
mintsuki 2022-01-28 08:35:06 +01:00
parent 55166fda2f
commit 580a2c3461
3 changed files with 27 additions and 63 deletions

View File

@ -219,7 +219,7 @@ limine-install:
.PHONY: clean
clean: limine-bios-clean limine-uefi32-clean limine-uefi64-clean
rm -rf '$(call SHESCAPE,$(BINDIR))' '$(call SHESCAPE,$(BUILDDIR))/stage1.stamp' '$(call SHESCAPE,$(BUILDDIR))/incbin'
rm -rf '$(call SHESCAPE,$(BINDIR))' '$(call SHESCAPE,$(BUILDDIR))/stage1.stamp' '$(call SHESCAPE,$(BUILDDIR))/incbin/incbin'
.PHONY: install-data
install-data:

View File

@ -1,33 +0,0 @@
#if defined (__APPLE__)
.data
.global __binary_limine_hdd_bin_start
__binary_limine_hdd_bin_start:
.incbin "limine-hdd.bin"
.global __binary_limine_hdd_bin_end
__binary_limine_hdd_bin_end:
#elif defined (__MINGW32__)
.section .data
.global __binary_limine_hdd_bin_start
__binary_limine_hdd_bin_start:
.incbin "limine-hdd.bin"
.global __binary_limine_hdd_bin_end
__binary_limine_hdd_bin_end:
#elif defined (__linux__) || defined (__unix__)
#if defined (__ELF__)
.section .note.GNU-stack, "", %progbits
#endif
.section .data
.global _binary_limine_hdd_bin_start
_binary_limine_hdd_bin_start:
.incbin "limine-hdd.bin"
.global _binary_limine_hdd_bin_end
_binary_limine_hdd_bin_end:
#endif

View File

@ -1,13 +1,3 @@
#undef IS_WINDOWS
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#define IS_WINDOWS 1
#endif
#ifndef IS_WINDOWS
#define _POSIX_C_SOURCE 200112L
#define _FILE_OFFSET_BITS 64
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
@ -15,13 +5,29 @@
#include <stdbool.h>
#include <string.h>
#include <inttypes.h>
#include <limits.h>
static inline int seek_(FILE *stream, int64_t offset, int whence) {
#ifdef IS_WINDOWS
return _fseeki64(stream, offset, whence);
#else
return fseeko(stream, offset, whence);
#endif
static int set_pos(FILE *stream, uint64_t pos) {
if (sizeof(long) >= 8) {
return fseek(stream, (long)pos, SEEK_SET);
}
long jump_size = (LONG_MAX / 2) + 1;
long last_jump = pos % jump_size;
uint64_t jumps = pos / jump_size;
rewind(stream);
for (uint64_t i = 0; i < jumps; i++) {
if (fseek(stream, jump_size, SEEK_CUR) != 0) {
return -1;
}
}
if (fseek(stream, last_jump, SEEK_CUR) != 0) {
return -1;
}
return 0;
}
#define DIV_ROUNDUP(a, b) (((a) + ((b) - 1)) / (b))
@ -142,10 +148,8 @@ static bool device_init(void) {
}
cache = tmp;
if (seek_(device, 0, SEEK_SET) == -1) {
perror("ERROR");
return false;
}
rewind(device);
size_t ret = fread(cache, guesses[i], 1, device);
if (ret != 1) {
continue;
@ -168,7 +172,7 @@ static bool device_flush_cache(void) {
if (cache_state == CACHE_CLEAN)
return true;
if (seek_(device, cached_block * block_size, SEEK_SET) == -1) {
if (set_pos(device, cached_block * block_size) != 0) {
perror("ERROR");
return false;
}
@ -192,7 +196,7 @@ static bool device_cache_block(uint64_t block) {
return false;
}
if (seek_(device, block * block_size, SEEK_SET) == -1) {
if (set_pos(device, block * block_size) != 0) {
perror("ERROR");
return false;
}
@ -280,13 +284,6 @@ int main(int argc, char *argv[]) {
size_t bootloader_file_size = (size_t)_binary_limine_hdd_bin_size;
uint8_t orig_mbr[70], timestamp[6];
#ifndef IS_WINDOWS
if (sizeof(off_t) != 8) {
fprintf(stderr, "ERROR: off_t type is not 64-bit.\n");
goto cleanup;
}
#endif
if (argc < 2) {
printf("Usage: %s <device> [GPT partition index]\n", argv[0]);
goto cleanup;