4 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include "blkid-api.h"
25 void usage(const char* msg) {
29 printf("USAGE: blkid-print device_name partition_label suffix\n"
30 "device_name: block device e.g. /dev/mmcblk0\n"
31 "partition_label: partiton label to search for e.g. rootfs,ramdisk etc.\n"
32 "suffix: a|b partiton slot\n");
35 blkid_partition partition_for(char *device_name, char *part_name, char *new_slot) {
36 char part_name_with_slot[MAX_PARTNAME_LEN];
37 int found_part_n = -1;
38 if (!device_name || !part_name || !new_slot){
39 printf("partition_for: one or more argument is NULL\n");
42 if (snprintf(part_name_with_slot, MAX_PARTNAME_LEN, "%s_%s", part_name, new_slot) < 0) {
45 blkid_probe pr = blkid_new_probe_from_filename(device_name);
47 printf("ERROR: blkid error for %s\n", device_name);
51 blkid_partlist ls = blkid_probe_get_partitions(pr);
53 printf("ERROR: unable to probe partitions.\n");
57 int nparts = blkid_partlist_numof_partitions(ls);
59 printf("ERROR: unable to get partition count\n");
65 for (int i = 0; i < nparts; i++) {
66 blkid_partition part = blkid_partlist_get_partition(ls, i);
69 p = blkid_partition_get_name(part);
73 if (found_part_n < 0 && strncmp(p, part_name, strlen(part_name)+1) == 0)
75 else if (strncmp(p, part_name_with_slot, strlen(part_name_with_slot)+1) == 0)
80 if (found_part_n >= 0) {
81 blkid_partition par = blkid_partlist_get_partition(ls, found_part_n);
87 const char *get_part_label(blkid_partlist ls, int part_nr) {
88 blkid_partition part = blkid_partlist_get_partition(ls, part_nr);
89 return blkid_partition_get_name(part);
92 int main(int argc, char *argv[]) {
93 char device_name_path[PATH_MAX];
95 usage("Please specify correct argument number\n");
98 char *device_name = realpath(argv[1], device_name_path);
100 printf("ERROR: Unable to determine realpath for: %s\n", argv[1]);
104 char *partition_label = argv[2];
105 char *suffix = argv[3];
106 blkid_partition part = partition_for(device_name, partition_label, suffix);
108 printf("ERROR: Partition '%s' not found.\n", partition_label);
112 int part_nr = blkid_partition_get_partno(part);
114 printf("ERROR: Partition '%s' not found", partition_label);
117 const char *part_label = blkid_partition_get_name(part);
118 char part_path[PATH_MAX];
119 // /dev/sda1 vs /dev/mmcblk0p1 /dev/nvme0n1p1
120 // no_separator vs "p" separator
121 if (strncmp("/dev/sd", device_name, strlen("/dev/sd")) == 0)
122 snprintf(part_path, PATH_MAX, "%s%d", device_name, part_nr);
124 snprintf(part_path, PATH_MAX, "%sp%d", device_name, part_nr);
125 printf("part_nr: %d (%s): %s\n", part_nr, part_label, part_path);