]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
part: Allow setting the partition-table type
authorSimon Glass <sjg@chromium.org>
Sun, 16 Jul 2023 03:38:47 +0000 (21:38 -0600)
committerBin Meng <bmeng@tinylab.org>
Mon, 17 Jul 2023 09:12:06 +0000 (17:12 +0800)
Some devices have multiple partition types available on the same media.
It is sometimes useful to see these to check that everything is working
correctly.

Provide a way to manually set the partition-table type, avoiding the
auto-detection process.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
cmd/part.c
disk/part.c
doc/usage/cmd/part.rst
include/part.h

index 28f2b7ff9bbe8a8252151a5f6ac40537169f1ff2..0ce190005d327d9879eee6b7ebd5f6797668427c 100644 (file)
@@ -182,6 +182,36 @@ static int do_part_number(int argc, char *const argv[])
        return do_part_info(argc, argv, CMD_PART_INFO_NUMBER);
 }
 
+static int do_part_set(int argc, char *const argv[])
+{
+       const char *devname, *partstr, *typestr;
+       struct blk_desc *desc;
+       int dev;
+
+       if (argc < 3)
+               return CMD_RET_USAGE;
+
+       /* Look up the device */
+       devname = argv[0];
+       partstr = argv[1];
+       typestr = argv[2];
+       dev = blk_get_device_by_str(devname, partstr, &desc);
+       if (dev < 0) {
+               printf("** Bad device specification %s %s **\n", devname,
+                      partstr);
+               return CMD_RET_FAILURE;
+       }
+
+       desc->part_type = part_get_type_by_name(typestr);
+       if (!desc->part_type) {
+               printf("Unknown partition type '%s'\n", typestr);
+               return CMD_RET_FAILURE;
+       }
+       part_print(desc);
+
+       return 0;
+}
+
 #ifdef CONFIG_PARTITION_TYPE_GUID
 static int do_part_type(int argc, char *const argv[])
 {
@@ -245,6 +275,8 @@ static int do_part(struct cmd_tbl *cmdtp, int flag, int argc,
                return do_part_number(argc - 2, argv + 2);
        else if (!strcmp(argv[1], "types"))
                return do_part_types(argc - 2, argv + 2);
+       else if (!strcmp(argv[1], "set"))
+               return do_part_set(argc - 2, argv + 2);
 #ifdef CONFIG_PARTITION_TYPE_GUID
        else if (!strcmp(argv[1], "type"))
                return do_part_type(argc - 2, argv + 2);
@@ -279,6 +311,8 @@ U_BOOT_CMD(
 #endif
        "part type <interface> <dev>:<part> <varname>\n"
        "    - set environment variable to partition type\n"
+       "part set <interface> <dev> type\n"
+       "    - set partition type for a device\n"
        "part types\n"
        "    - list supported partition table types"
 );
index 35300df5903947f5211f6c45d8da1d99cc7c68c7..1d2117ab71ee960967c574e0e99f11c31c92bab8 100644 (file)
@@ -54,6 +54,22 @@ static struct part_driver *part_driver_lookup_type(struct blk_desc *dev_desc)
        return NULL;
 }
 
+int part_get_type_by_name(const char *name)
+{
+       struct part_driver *drv =
+               ll_entry_start(struct part_driver, part_driver);
+       const int n_ents = ll_entry_count(struct part_driver, part_driver);
+       struct part_driver *entry;
+
+       for (entry = drv; entry != drv + n_ents; entry++) {
+               if (!strcasecmp(name, entry->name))
+                       return entry->part_type;
+       }
+
+       /* Not found */
+       return PART_TYPE_UNKNOWN;
+}
+
 static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
 {
        struct blk_desc *dev_desc;
index 8d2a2803912db0dcb18e9253611435310fc36de4..8a594aaff27ed2b606c2237fe9f9296b9653e981 100644 (file)
@@ -13,6 +13,7 @@ Synopis
     part start <interface> <dev> <part> <varname>
     part size <interface> <dev> <part> <varname>
     part number <interface> <dev> <part> <varname>
+    part set <interface> <dev> <part> <type>
     part type <interface> <dev>:<part> [varname]
     part types
 
@@ -82,6 +83,18 @@ part must be specified as partition name.
     varname
         a variable to store the current partition number value into
 
+The 'part set' command sets the type of a partition. This is useful when
+autodetection fails or does not do the correct thing:
+
+    interface
+        interface for accessing the block device (mmc, sata, scsi, usb, ....)
+    dev
+        device number
+    part
+        partition number
+    type
+        partition type to use (see 'part types') to check available types
+
 The 'part type' command prints or sets an environment variable to the partition type UUID.
 
     interface
@@ -147,6 +160,67 @@ Examples
     => part types
     Supported partition tables: EFI, AMIGA, DOS, ISO, MAC
 
+This shows looking at a device with multiple partition tables::
+
+    => virtio scan
+    => part list virtio 0
+
+    Partition Map for VirtIO device 0  --   Partition Type: EFI
+
+    Part       Start LBA       End LBA         Name
+            Attributes
+            Type GUID
+            Partition GUID
+    1  0x00000040      0x0092b093      "ISO9660"
+            attrs:     0x1000000000000001
+            type:      ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
+            guid:      a0891d7e-b930-4513-94d8-f629dbd637b2
+    2  0x0092b094      0x0092d7e7      "Appended2"
+            attrs:     0x0000000000000000
+            type:      c12a7328-f81f-11d2-ba4b-00a0c93ec93b
+            guid:      a0891d7e-b930-4513-94db-f629dbd637b2
+    3  0x0092d7e8      0x0092da3f      "Gap1"
+            attrs:     0x1000000000000001
+            type:      ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
+            guid:      a0891d7e-b930-4513-94da-f629dbd637b2
+    => ls virtio 0:3
+    => part types
+    Supported partition tables: EFI, DOS, ISO
+    => part set virtio 0 dos
+
+    Partition Map for VirtIO device 0  --   Partition Type: DOS
+
+    Part       Start Sector    Num Sectors     UUID            Type
+    1  1               9624191         00000000-01     ee
+    => part set virtio 0 iso
+
+    Partition Map for VirtIO device 0  --   Partition Type: ISO
+
+    Part   Start     Sect x Size Type
+    1     3020        4    512 U-Boot
+    2  9613460    10068    512 U-Boot
+    => part set virtio 0 efi
+
+    Partition Map for VirtIO device 0  --   Partition Type: EFI
+
+    Part       Start LBA       End LBA         Name
+            Attributes
+            Type GUID
+            Partition GUID
+    1  0x00000040      0x0092b093      "ISO9660"
+            attrs:     0x1000000000000001
+            type:      ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
+            guid:      a0891d7e-b930-4513-94d8-f629dbd637b2
+    2  0x0092b094      0x0092d7e7      "Appended2"
+            attrs:     0x0000000000000000
+            type:      c12a7328-f81f-11d2-ba4b-00a0c93ec93b
+            guid:      a0891d7e-b930-4513-94db-f629dbd637b2
+    3  0x0092d7e8      0x0092da3f      "Gap1"
+            attrs:     0x1000000000000001
+            type:      ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
+            guid:      a0891d7e-b930-4513-94da-f629dbd637b2
+    =>
+
 Return value
 ------------
 
index be75c73549554f530b8539d81c0bff0232e22c98..3b1b5398699ca213892443943fdd29c01694e3cd 100644 (file)
@@ -598,6 +598,15 @@ static inline struct part_driver *part_driver_get_first(void)
        return ll_entry_start(struct part_driver, part_driver);
 }
 
+/**
+ * part_get_type_by_name() - Get partition type by name
+ *
+ * @name: Name of partition type to look up (not case-sensitive)
+ * Returns: Corresponding partition type (PART_TYPE_...) or PART_TYPE_UNKNOWN if
+ * not known
+ */
+int part_get_type_by_name(const char *name);
+
 #else
 static inline int part_driver_get_count(void)
 { return 0; }