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