From fea8b881af838b21886c5093540e1775c9b26a4f Mon Sep 17 00:00:00 2001 From: Alexander Myltsev Date: Sat, 10 Feb 2024 01:39:02 +0400 Subject: [PATCH] Do not use `cat` or `type`. --- Makefile | 7 ++----- tools/mbrpad.c | 28 ++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index f0009a9..209951a 100644 --- a/Makefile +++ b/Makefile @@ -105,9 +105,6 @@ LDFLAGS=-m elf_i386 user/%: user/%.o user/crt.o $(LD) $(LDFLAGS) -o $@ -Ttext 0x1000 $^ -image.bin: mbr.bin fs.img - $(CAT) $^ >$@ - kernel.bin: $(OBJECTS) $(LD) $(LDFLAGS) $(LDKERNELFLAGS) -o $@ -Ttext 0x9000 $^ @@ -120,9 +117,9 @@ bootmain.o: bootmain.c %.o: %.S $(CC) $(ASMFLAGS) $^ -o $@ -mbr.bin: mbr.elf tools/mbrpad +image.bin: mbr.elf tools/mbrpad fs.img $(OBJCOPY) -S -O binary -j .text $< $@ - tools/mbrpad $@ + tools/mbrpad $@ fs.img mbr.raw: mbr.o bootmain.o $(LD) -N -m elf_i386 -Ttext=0x7c00 --oformat=binary $^ -o $@ diff --git a/tools/mbrpad.c b/tools/mbrpad.c index 4f2059d..433835a 100644 --- a/tools/mbrpad.c +++ b/tools/mbrpad.c @@ -3,20 +3,36 @@ #include #include +enum { + MBR_END = 510, +}; + int main(int argc, char* argv[]) { - if (argc != 2) { - fprintf(stderr, "Usage: %s mbr.bin\n", argv[0]); + if (argc != 3) { + fprintf(stderr, "Usage: %s mbr.bin fs.img\n", argv[0]); return 1; } const char* filename = argv[1]; + const char* fsimg_path = argv[2]; int fd = open(filename, O_RDWR); off_t length = lseek(fd, 0, SEEK_END); - if (length > 510) { - fprintf(stderr, "file %s is larger than 510 bytes (size: %llu)\n", - filename, (unsigned long long)length); + if (length > MBR_END) { + fprintf(stderr, "file %s is larger than %d bytes (size: %llu)\n", + filename, MBR_END, (unsigned long long)length); return 1; } - lseek(fd, 510, SEEK_SET); + lseek(fd, MBR_END, SEEK_SET); write(fd, "\x55\xaa", 2); + + int fsimg = open(fsimg_path, O_RDONLY); + if (fsimg < 0) { + perror(fsimg_path); + return 1; + } + char buf[1024]; + ssize_t bytes; + while ((bytes = read(fsimg, buf, sizeof(buf))) > 0) { + write(fd, buf, bytes); + } return 0; }