1*180cc7c6SAlex Deymo /*
2*180cc7c6SAlex Deymo * Copyright (C) 2017 The Android Open Source Project
3*180cc7c6SAlex Deymo *
4*180cc7c6SAlex Deymo * SPDX-License-Identifier: BSD-2-Clause
5*180cc7c6SAlex Deymo */
6*180cc7c6SAlex Deymo
7*180cc7c6SAlex Deymo #include <android_cmds.h>
8*180cc7c6SAlex Deymo
9*180cc7c6SAlex Deymo #include <common.h>
10*180cc7c6SAlex Deymo #include <part.h>
11*180cc7c6SAlex Deymo
12*180cc7c6SAlex Deymo /**
13*180cc7c6SAlex Deymo * part_get_info_by_dev_and_name - Parse a device number and partition name
14*180cc7c6SAlex Deymo * string in the form of "device_num;partition_name", for example "0;misc".
15*180cc7c6SAlex Deymo * If the partition is found, sets dev_desc and part_info accordingly with the
16*180cc7c6SAlex Deymo * information of the partition with the given partition_name.
17*180cc7c6SAlex Deymo *
18*180cc7c6SAlex Deymo * @dev_iface: Device interface.
19*180cc7c6SAlex Deymo * @dev_part_str: Input string argument, like "0;misc".
20*180cc7c6SAlex Deymo * @dev_desc: Place to store the device description pointer.
21*180cc7c6SAlex Deymo * @part_info: Place to store the partition information.
22*180cc7c6SAlex Deymo * @return 0 on success, or -1 on error
23*180cc7c6SAlex Deymo */
part_get_info_by_dev_and_name(const char * dev_iface,const char * dev_part_str,struct blk_desc ** dev_desc,disk_partition_t * part_info)24*180cc7c6SAlex Deymo static int part_get_info_by_dev_and_name(const char *dev_iface,
25*180cc7c6SAlex Deymo const char *dev_part_str,
26*180cc7c6SAlex Deymo struct blk_desc **dev_desc,
27*180cc7c6SAlex Deymo disk_partition_t *part_info)
28*180cc7c6SAlex Deymo {
29*180cc7c6SAlex Deymo char *ep;
30*180cc7c6SAlex Deymo const char *part_str;
31*180cc7c6SAlex Deymo int dev_num;
32*180cc7c6SAlex Deymo
33*180cc7c6SAlex Deymo part_str = strchr(dev_part_str, ';');
34*180cc7c6SAlex Deymo if (!part_str || part_str == dev_part_str)
35*180cc7c6SAlex Deymo return -1;
36*180cc7c6SAlex Deymo
37*180cc7c6SAlex Deymo dev_num = simple_strtoul(dev_part_str, &ep, 16);
38*180cc7c6SAlex Deymo if (ep != part_str) {
39*180cc7c6SAlex Deymo /* Not all the first part before the ; was parsed. */
40*180cc7c6SAlex Deymo return -1;
41*180cc7c6SAlex Deymo }
42*180cc7c6SAlex Deymo part_str++;
43*180cc7c6SAlex Deymo
44*180cc7c6SAlex Deymo *dev_desc = blk_get_dev(dev_iface, dev_num);
45*180cc7c6SAlex Deymo if (!*dev_desc) {
46*180cc7c6SAlex Deymo printf("Could not find %s %d\n", dev_iface, dev_num);
47*180cc7c6SAlex Deymo return -1;
48*180cc7c6SAlex Deymo }
49*180cc7c6SAlex Deymo if (part_get_info_by_name(*dev_desc, part_str, part_info) < 0) {
50*180cc7c6SAlex Deymo printf("Could not find \"%s\" partition\n", part_str);
51*180cc7c6SAlex Deymo return -1;
52*180cc7c6SAlex Deymo }
53*180cc7c6SAlex Deymo return 0;
54*180cc7c6SAlex Deymo }
55*180cc7c6SAlex Deymo
part_get_info_by_dev_and_name_or_num(const char * dev_iface,const char * dev_part_str,struct blk_desc ** dev_desc,disk_partition_t * part_info)56*180cc7c6SAlex Deymo int part_get_info_by_dev_and_name_or_num(const char *dev_iface,
57*180cc7c6SAlex Deymo const char *dev_part_str,
58*180cc7c6SAlex Deymo struct blk_desc **dev_desc,
59*180cc7c6SAlex Deymo disk_partition_t *part_info) {
60*180cc7c6SAlex Deymo /* Split the part_name if passed as "$dev_num;part_name". */
61*180cc7c6SAlex Deymo if (!part_get_info_by_dev_and_name(dev_iface, dev_part_str,
62*180cc7c6SAlex Deymo dev_desc, part_info))
63*180cc7c6SAlex Deymo return 0;
64*180cc7c6SAlex Deymo /* Couldn't lookup by name, try looking up the partition description
65*180cc7c6SAlex Deymo * directly.
66*180cc7c6SAlex Deymo */
67*180cc7c6SAlex Deymo if (blk_get_device_part_str(dev_iface, dev_part_str,
68*180cc7c6SAlex Deymo dev_desc, part_info, 1) < 0) {
69*180cc7c6SAlex Deymo printf("Couldn't find partition %s %s\n",
70*180cc7c6SAlex Deymo dev_iface, dev_part_str);
71*180cc7c6SAlex Deymo return -1;
72*180cc7c6SAlex Deymo }
73*180cc7c6SAlex Deymo return 0;
74*180cc7c6SAlex Deymo }
75