diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b57b75f --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +CC = gcc +CFLAGS = -Os +LDFLAGS = -lz + +all: ps5_control m2_init + +ps5_control: ps5_control.c + $(CC) $(CFLAGS) -o ps5_control ps5_control.c + +m2_init: m2_init.c + $(CC) $(CFLAGS) -o m2_init m2_init.c $(LDFLAGS) + +clean: + rm -f ps5_control m2_init diff --git a/m2_exec.sh b/m2_exec.sh index 0dc1fdf..701fc03 100755 --- a/m2_exec.sh +++ b/m2_exec.sh @@ -14,7 +14,7 @@ if [ -z "$ROOT_LABEL" ]; then exit 1 fi -echo "Preparing to kexec into $NVME_PART (Label: $ROOT_LABEL)..." +echo "Mounting $NVME_PART ($ROOT_LABEL)..." mkdir -p "$MNT" mount -o ro "$NVME_PART" "$MNT" @@ -27,17 +27,21 @@ if [[ ! -f "$VMLINUZ" ]]; then INITRD=$(ls -v $MNT/boot/initrd.img* | tail -n 1) fi -echo "Using kernel: $VMLINUZ" -echo "Using initrd: $INITRD" +echo "kernel: $VMLINUZ" +echo "initrd: $INITRD" -CURRENT_CMD=$(cat /proc/cmdline | sed -e 's/root=[^ ]*//g') +CMDLINE=$(cat /proc/cmdline | \ + sed -e 's/video=[^ ]*//g' \ + -e "s|root=[^ ]*|root=LABEL=$ROOT_LABEL|g" | \ + xargs) + +echo "cmdline: $CMDLINE" kexec -l "$VMLINUZ" \ --initrd="$INITRD" \ - --append="$CURRENT_CMD root=LABEL=$ROOT_LABEL" + --append="$CMDLINE" -echo "Unmounting and executing... See you on the other side." +echo "Unmounting and executing..." umount "$MNT" systemctl kexec -i - diff --git a/m2_init.c b/m2_init.c index 804bdb3..94c12a3 100644 --- a/m2_init.c +++ b/m2_init.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -103,13 +104,13 @@ int main(int argc, char *argv[]) { fd = open("/dev/nvme0n1", O_RDWR); if (fd == -1) { perror("open"); - return 1; + return EXIT_FAILURE; } if (ioctl(fd, BLKGETSIZE64, &bytes) == -1) { perror("ioctl"); close(fd); - return 1; + return EXIT_FAILURE; } uint64_t total_sectors = bytes / SECTOR_SIZE; @@ -167,14 +168,31 @@ int main(int argc, char *argv[]) { printf("\nPrimary GPT Header (LBA %" PRIu64 "):\n", gpt->my_lba); printf(" Signature: 0x%" PRIx64 "\n", gpt->signature); printf(" Header CRC32: 0x%08x\n", gpt->header_crc32); - printf(" Partition Entry Array CRC32: 0x%08x\n", gpt->partition_entry_array_crc32); + printf(" Partition Entry Array CRC32: 0x%08x\n", + gpt->partition_entry_array_crc32); printf(" Partition Entry LBA: %" PRIu64 "\n", gpt->partition_entry_lba); // Write mbr and primary gpt. - lseek(fd, (off_t)PS5_M2_MBR_LBA * SECTOR_SIZE, SEEK_SET); - write(fd, &mbr, SECTOR_SIZE); - write(fd, gpt_sector, SECTOR_SIZE); - write(fd, entries, sizeof(entries)); + if (lseek(fd, (off_t)PS5_M2_MBR_LBA * SECTOR_SIZE, SEEK_SET) == (off_t)-1) { + perror("lseek"); + close(fd); + return EXIT_FAILURE; + } + if (write(fd, &mbr, SECTOR_SIZE) != SECTOR_SIZE) { + perror("write"); + close(fd); + return EXIT_FAILURE; + } + if (write(fd, gpt_sector, SECTOR_SIZE) != SECTOR_SIZE) { + perror("write"); + close(fd); + return EXIT_FAILURE; + } + if (write(fd, entries, sizeof(entries)) != sizeof(entries)) { + perror("write"); + close(fd); + return EXIT_FAILURE; + } gpt->my_lba = lastlba; gpt->alternate_lba = PS5_M2_MBR_LBA + GPT_PRIMARY_PARTITION_TABLE_LBA; @@ -187,13 +205,30 @@ int main(int argc, char *argv[]) { printf(" Partition Entry LBA: %" PRIu64 "\n", gpt->partition_entry_lba); // Write alternate gpt. - lseek(fd, (off_t)(lastlba - GPT_ENTRY_NUMBERS) * SECTOR_SIZE, SEEK_SET); - write(fd, entries, sizeof(entries)); - lseek(fd, (off_t)lastlba * SECTOR_SIZE, SEEK_SET); - write(fd, gpt_sector, SECTOR_SIZE); + if (lseek(fd, (off_t)(lastlba - GPT_ENTRY_NUMBERS) * SECTOR_SIZE, SEEK_SET) == + (off_t)-1) { + perror("lseek"); + close(fd); + return EXIT_FAILURE; + } + if (write(fd, entries, sizeof(entries)) != sizeof(entries)) { + perror("write"); + close(fd); + return EXIT_FAILURE; + } + if (lseek(fd, (off_t)lastlba * SECTOR_SIZE, SEEK_SET) == (off_t)-1) { + perror("lseek"); + close(fd); + return EXIT_FAILURE; + } + if (write(fd, gpt_sector, SECTOR_SIZE) != SECTOR_SIZE) { + perror("write"); + close(fd); + return EXIT_FAILURE; + } printf("\nSuccessfully added Linux partition to m2.\n"); close(fd); - return 0; + return EXIT_SUCCESS; }