Skip to content

Commit 125194e

Browse files
sjg20lbmeng
authored andcommitted
part: Allow setting the partition-table type
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 <[email protected]> Reviewed-by: Bin Meng <[email protected]>
1 parent b279f51 commit 125194e

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

cmd/part.c

+34
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,36 @@ static int do_part_number(int argc, char *const argv[])
182182
return do_part_info(argc, argv, CMD_PART_INFO_NUMBER);
183183
}
184184

185+
static int do_part_set(int argc, char *const argv[])
186+
{
187+
const char *devname, *partstr, *typestr;
188+
struct blk_desc *desc;
189+
int dev;
190+
191+
if (argc < 3)
192+
return CMD_RET_USAGE;
193+
194+
/* Look up the device */
195+
devname = argv[0];
196+
partstr = argv[1];
197+
typestr = argv[2];
198+
dev = blk_get_device_by_str(devname, partstr, &desc);
199+
if (dev < 0) {
200+
printf("** Bad device specification %s %s **\n", devname,
201+
partstr);
202+
return CMD_RET_FAILURE;
203+
}
204+
205+
desc->part_type = part_get_type_by_name(typestr);
206+
if (!desc->part_type) {
207+
printf("Unknown partition type '%s'\n", typestr);
208+
return CMD_RET_FAILURE;
209+
}
210+
part_print(desc);
211+
212+
return 0;
213+
}
214+
185215
#ifdef CONFIG_PARTITION_TYPE_GUID
186216
static int do_part_type(int argc, char *const argv[])
187217
{
@@ -245,6 +275,8 @@ static int do_part(struct cmd_tbl *cmdtp, int flag, int argc,
245275
return do_part_number(argc - 2, argv + 2);
246276
else if (!strcmp(argv[1], "types"))
247277
return do_part_types(argc - 2, argv + 2);
278+
else if (!strcmp(argv[1], "set"))
279+
return do_part_set(argc - 2, argv + 2);
248280
#ifdef CONFIG_PARTITION_TYPE_GUID
249281
else if (!strcmp(argv[1], "type"))
250282
return do_part_type(argc - 2, argv + 2);
@@ -279,6 +311,8 @@ U_BOOT_CMD(
279311
#endif
280312
"part type <interface> <dev>:<part> <varname>\n"
281313
" - set environment variable to partition type\n"
314+
"part set <interface> <dev> type\n"
315+
" - set partition type for a device\n"
282316
"part types\n"
283317
" - list supported partition table types"
284318
);

disk/part.c

+16
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ static struct part_driver *part_driver_lookup_type(struct blk_desc *dev_desc)
5454
return NULL;
5555
}
5656

57+
int part_get_type_by_name(const char *name)
58+
{
59+
struct part_driver *drv =
60+
ll_entry_start(struct part_driver, part_driver);
61+
const int n_ents = ll_entry_count(struct part_driver, part_driver);
62+
struct part_driver *entry;
63+
64+
for (entry = drv; entry != drv + n_ents; entry++) {
65+
if (!strcasecmp(name, entry->name))
66+
return entry->part_type;
67+
}
68+
69+
/* Not found */
70+
return PART_TYPE_UNKNOWN;
71+
}
72+
5773
static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
5874
{
5975
struct blk_desc *dev_desc;

doc/usage/cmd/part.rst

+74
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Synopis
1313
part start <interface> <dev> <part> <varname>
1414
part size <interface> <dev> <part> <varname>
1515
part number <interface> <dev> <part> <varname>
16+
part set <interface> <dev> <part> <type>
1617
part type <interface> <dev>:<part> [varname]
1718
part types
1819

@@ -82,6 +83,18 @@ part must be specified as partition name.
8283
varname
8384
a variable to store the current partition number value into
8485

86+
The 'part set' command sets the type of a partition. This is useful when
87+
autodetection fails or does not do the correct thing:
88+
89+
interface
90+
interface for accessing the block device (mmc, sata, scsi, usb, ....)
91+
dev
92+
device number
93+
part
94+
partition number
95+
type
96+
partition type to use (see 'part types') to check available types
97+
8598
The 'part type' command prints or sets an environment variable to the partition type UUID.
8699

87100
interface
@@ -147,6 +160,67 @@ Examples
147160
=> part types
148161
Supported partition tables: EFI, AMIGA, DOS, ISO, MAC
149162

163+
This shows looking at a device with multiple partition tables::
164+
165+
=> virtio scan
166+
=> part list virtio 0
167+
168+
Partition Map for VirtIO device 0 -- Partition Type: EFI
169+
170+
Part Start LBA End LBA Name
171+
Attributes
172+
Type GUID
173+
Partition GUID
174+
1 0x00000040 0x0092b093 "ISO9660"
175+
attrs: 0x1000000000000001
176+
type: ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
177+
guid: a0891d7e-b930-4513-94d8-f629dbd637b2
178+
2 0x0092b094 0x0092d7e7 "Appended2"
179+
attrs: 0x0000000000000000
180+
type: c12a7328-f81f-11d2-ba4b-00a0c93ec93b
181+
guid: a0891d7e-b930-4513-94db-f629dbd637b2
182+
3 0x0092d7e8 0x0092da3f "Gap1"
183+
attrs: 0x1000000000000001
184+
type: ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
185+
guid: a0891d7e-b930-4513-94da-f629dbd637b2
186+
=> ls virtio 0:3
187+
=> part types
188+
Supported partition tables: EFI, DOS, ISO
189+
=> part set virtio 0 dos
190+
191+
Partition Map for VirtIO device 0 -- Partition Type: DOS
192+
193+
Part Start Sector Num Sectors UUID Type
194+
1 1 9624191 00000000-01 ee
195+
=> part set virtio 0 iso
196+
197+
Partition Map for VirtIO device 0 -- Partition Type: ISO
198+
199+
Part Start Sect x Size Type
200+
1 3020 4 512 U-Boot
201+
2 9613460 10068 512 U-Boot
202+
=> part set virtio 0 efi
203+
204+
Partition Map for VirtIO device 0 -- Partition Type: EFI
205+
206+
Part Start LBA End LBA Name
207+
Attributes
208+
Type GUID
209+
Partition GUID
210+
1 0x00000040 0x0092b093 "ISO9660"
211+
attrs: 0x1000000000000001
212+
type: ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
213+
guid: a0891d7e-b930-4513-94d8-f629dbd637b2
214+
2 0x0092b094 0x0092d7e7 "Appended2"
215+
attrs: 0x0000000000000000
216+
type: c12a7328-f81f-11d2-ba4b-00a0c93ec93b
217+
guid: a0891d7e-b930-4513-94db-f629dbd637b2
218+
3 0x0092d7e8 0x0092da3f "Gap1"
219+
attrs: 0x1000000000000001
220+
type: ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
221+
guid: a0891d7e-b930-4513-94da-f629dbd637b2
222+
=>
223+
150224
Return value
151225
------------
152226

include/part.h

+9
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,15 @@ static inline struct part_driver *part_driver_get_first(void)
598598
return ll_entry_start(struct part_driver, part_driver);
599599
}
600600

601+
/**
602+
* part_get_type_by_name() - Get partition type by name
603+
*
604+
* @name: Name of partition type to look up (not case-sensitive)
605+
* Returns: Corresponding partition type (PART_TYPE_...) or PART_TYPE_UNKNOWN if
606+
* not known
607+
*/
608+
int part_get_type_by_name(const char *name);
609+
601610
#else
602611
static inline int part_driver_get_count(void)
603612
{ return 0; }

0 commit comments

Comments
 (0)