From 187629b228d9b2f4a093b89737eaa9da57e3779e Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Fri, 20 Mar 2026 22:34:05 +0100 Subject: [PATCH] sdutil Add list_part_dos command --- sdutil/sdutil.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/sdutil/sdutil.c b/sdutil/sdutil.c index 7567a0e..ccc46bc 100644 --- a/sdutil/sdutil.c +++ b/sdutil/sdutil.c @@ -8,8 +8,6 @@ #include #include -#define COMMAND_MAX 32 - struct dos_pte { uint8_t drive_attrs; uint8_t chs_start_addr[3]; @@ -98,11 +96,37 @@ static void start_part_dos (void) { } } +static void list_part_dos (const char* dev_name) { + struct dos_mbr mbr; + memset (&mbr, 0, sizeof (mbr)); + + size_t sector = 0; + size_t sector_count = 1; + int r = device_do (dev_name, XDRV_READ, §or, §or_count, &mbr, NULL); + mprintf ("Finished reading. Result: %s (%d)\n", str_status[r < 0 ? -r : r], r); + + if (!(mbr.valid_sign[0] == 0x55 && mbr.valid_sign[1] == 0xAA)) { + mprintf ("ERROR invalid Master Boot Record!\n"); + return; + } + + /* clang-format off */ + mprintf ("\n\n-------------------------------------------------------\n"); + mprintf ("Summary (%s):\n", dev_name); + mprintf ("Partition 0: start LBA = %u, sector count = %zu\n",mbr.ptes[0].start_lba, mbr.ptes[0].sector_count); + mprintf ("Partition 1: start LBA = %u, sector count = %zu\n",mbr.ptes[1].start_lba, mbr.ptes[1].sector_count); + mprintf ("Partition 2: start LBA = %u, sector count = %zu\n",mbr.ptes[2].start_lba, mbr.ptes[2].sector_count); + mprintf ("Partition 3: start LBA = %u, sector count = %zu\n",mbr.ptes[3].start_lba, mbr.ptes[3].sector_count); + /* clang-format on */ +} + void app_main (void) { libprocess_self_init (); - char commandbuf[COMMAND_MAX]; + char commandbuf[32]; commandbuf[0] = '\0'; + char devnamebuf[64]; + devnamebuf[0] = '\0'; if (env_get (process_get_pgid (), "C", (void*)commandbuf, sizeof (commandbuf)) != ST_OK) { mprintf ("ERROR C=???. No command provided\n"); @@ -111,5 +135,14 @@ void app_main (void) { if (strcmp (commandbuf, "part_dos") == 0) { start_part_dos (); + } else if (strcmp (commandbuf, "list_part_dos") == 0) { + if (env_get (process_get_pgid (), "dev", (void*)devnamebuf, sizeof (devnamebuf)) != ST_OK) { + mprintf ("ERROR dev=???. No device name provided for list_part_dos\n"); + return; + } + + list_part_dos (devnamebuf); + } else { + mprintf ("ERROR C=%s. Unknown command\n", commandbuf); } }