Improve m2_exec script and add Makefile.

This commit is contained in:
Andy Nguyen
2026-05-02 20:09:25 +02:00
parent 86f452f5a1
commit 8d364bdaab
3 changed files with 72 additions and 19 deletions

14
Makefile Normal file
View File

@@ -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

View File

@@ -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

View File

@@ -3,6 +3,7 @@
#include <linux/fs.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
@@ -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;
}