blob: d04588ec22c727afe805bbb7db1f7a851f81a6fa [file] [log] [blame]
Stephen Warren5cf41dc2012-09-21 09:51:01 +00001/*
2 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
3 *
4 * made from cmd_ext2, which was:
5 *
6 * (C) Copyright 2004
7 * esd gmbh <www.esd-electronics.com>
8 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
9 *
10 * made from cmd_reiserfs by
11 *
12 * (C) Copyright 2003 - 2004
13 * Sysgo Real-Time Solutions, AG <www.elinos.com>
14 * Pavel Bartusek <pba@sysgo.com>
15 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020016 * SPDX-License-Identifier: GPL-2.0+
Stephen Warren5cf41dc2012-09-21 09:51:01 +000017 */
18
19#include <common.h>
20#include <config.h>
21#include <command.h>
22#include <part.h>
23#include <vsprintf.h>
24
25#ifndef CONFIG_PARTITION_UUIDS
26#error CONFIG_PARTITION_UUIDS must be enabled for CONFIG_CMD_PART to be enabled
27#endif
28
Jeroen Hofstee0e350f82014-06-23 00:22:08 +020029static int do_part_uuid(int argc, char * const argv[])
Stephen Warren5cf41dc2012-09-21 09:51:01 +000030{
31 int part;
32 block_dev_desc_t *dev_desc;
33 disk_partition_t info;
34
35 if (argc < 2)
36 return CMD_RET_USAGE;
37 if (argc > 3)
38 return CMD_RET_USAGE;
39
40 part = get_device_and_partition(argv[0], argv[1], &dev_desc, &info, 0);
41 if (part < 0)
42 return 1;
43
44 if (argc > 2)
45 setenv(argv[2], info.uuid);
46 else
47 printf("%s\n", info.uuid);
48
49 return 0;
50}
51
Jeroen Hofstee0e350f82014-06-23 00:22:08 +020052static int do_part_list(int argc, char * const argv[])
Stephen Warren5cf41dc2012-09-21 09:51:01 +000053{
54 int ret;
55 block_dev_desc_t *desc;
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010056 char *var = NULL;
57 bool bootable = false;
58 int i;
Stephen Warren5cf41dc2012-09-21 09:51:01 +000059
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010060 if (argc < 2)
Stephen Warren5cf41dc2012-09-21 09:51:01 +000061 return CMD_RET_USAGE;
62
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010063 if (argc > 2) {
64 for (i = 2; i < argc ; i++) {
65 if (argv[i][0] == '-') {
66 if (!strcmp(argv[i], "-bootable")) {
67 bootable = true;
68 } else {
69 printf("Unknown option %s\n", argv[i]);
70 return CMD_RET_USAGE;
71 }
72 } else {
73 var = argv[i];
74 break;
75 }
76 }
77
78 /* Loops should have been exited at the last argument, which
79 * as it contained the variable */
80 if (argc != i + 1)
81 return CMD_RET_USAGE;
82 }
83
Stephen Warren5cf41dc2012-09-21 09:51:01 +000084 ret = get_device(argv[0], argv[1], &desc);
85 if (ret < 0)
86 return 1;
87
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010088 if (var != NULL) {
Sjoerd Simonse86df6e2015-01-05 18:13:37 +010089 int p;
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010090 char str[512] = { '\0', };
Sjoerd Simonse86df6e2015-01-05 18:13:37 +010091 disk_partition_t info;
92
93 for (p = 1; p < 128; p++) {
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010094 char t[5];
Sjoerd Simonse86df6e2015-01-05 18:13:37 +010095 int r = get_partition_info(desc, p, &info);
96
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010097 if (r != 0)
98 continue;
99
100 if (bootable && !info.bootable)
101 continue;
102
103 sprintf(t, "%s%d", str[0] ? " " : "", p);
104 strcat(str, t);
Sjoerd Simonse86df6e2015-01-05 18:13:37 +0100105 }
Sjoerd Simons0798d6f2015-02-25 23:23:50 +0100106 setenv(var, str);
Sjoerd Simonse86df6e2015-01-05 18:13:37 +0100107 return 0;
108 }
109
Stephen Warren5cf41dc2012-09-21 09:51:01 +0000110 print_part(desc);
111
112 return 0;
113}
114
Jeroen Hofstee0e350f82014-06-23 00:22:08 +0200115static int do_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Stephen Warren5cf41dc2012-09-21 09:51:01 +0000116{
117 if (argc < 2)
118 return CMD_RET_USAGE;
119
120 if (!strcmp(argv[1], "uuid"))
121 return do_part_uuid(argc - 2, argv + 2);
122 else if (!strcmp(argv[1], "list"))
123 return do_part_list(argc - 2, argv + 2);
124
125 return CMD_RET_USAGE;
126}
127
128U_BOOT_CMD(
Sjoerd Simons0798d6f2015-02-25 23:23:50 +0100129 part, CONFIG_SYS_MAXARGS, 1, do_part,
Stephen Warren5cf41dc2012-09-21 09:51:01 +0000130 "disk partition related commands",
Stephen Warren8210a342014-05-07 12:19:00 -0600131 "part uuid <interface> <dev>:<part>\n"
Stephen Warren5cf41dc2012-09-21 09:51:01 +0000132 " - print partition UUID\n"
133 "part uuid <interface> <dev>:<part> <varname>\n"
134 " - set environment variable to partition UUID\n"
135 "part list <interface> <dev>\n"
Sjoerd Simonse86df6e2015-01-05 18:13:37 +0100136 " - print a device's partition table\n"
Sjoerd Simons0798d6f2015-02-25 23:23:50 +0100137 "part list <interface> <dev> [flags] <varname>\n"
138 " - set environment variable to the list of partitions\n"
139 " flags can be -bootable (list only bootable partitions)"
Stephen Warren5cf41dc2012-09-21 09:51:01 +0000140);