1*9bcc1fc0SBob Badour /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2*9bcc1fc0SBob Badour * Use of this source code is governed by a BSD-style license that can be
3*9bcc1fc0SBob Badour * found in the LICENSE file.
4*9bcc1fc0SBob Badour *
5*9bcc1fc0SBob Badour * Implements root device discovery via sysfs with optional bells and whistles.
6*9bcc1fc0SBob Badour */
7*9bcc1fc0SBob Badour
8*9bcc1fc0SBob Badour #include "rootdev.h"
9*9bcc1fc0SBob Badour
10*9bcc1fc0SBob Badour #include <ctype.h>
11*9bcc1fc0SBob Badour #include <dirent.h>
12*9bcc1fc0SBob Badour #include <err.h>
13*9bcc1fc0SBob Badour #include <errno.h>
14*9bcc1fc0SBob Badour #include <fcntl.h>
15*9bcc1fc0SBob Badour #include <stdbool.h>
16*9bcc1fc0SBob Badour #include <stddef.h>
17*9bcc1fc0SBob Badour #include <stdio.h>
18*9bcc1fc0SBob Badour #include <stdlib.h>
19*9bcc1fc0SBob Badour #include <string.h>
20*9bcc1fc0SBob Badour #include <sys/stat.h>
21*9bcc1fc0SBob Badour #include <sys/types.h>
22*9bcc1fc0SBob Badour #include <unistd.h>
23*9bcc1fc0SBob Badour
24*9bcc1fc0SBob Badour /*
25*9bcc1fc0SBob Badour * Limit prevents endless looping to find slave.
26*9bcc1fc0SBob Badour * We currently have at most 2 levels, this allows
27*9bcc1fc0SBob Badour * for future growth.
28*9bcc1fc0SBob Badour */
29*9bcc1fc0SBob Badour #define MAX_SLAVE_DEPTH 8
30*9bcc1fc0SBob Badour
31*9bcc1fc0SBob Badour static const char *kDefaultSearchPath = "/sys/block";
32*9bcc1fc0SBob Badour static const char *kDefaultDevPath = "/dev/block";
33*9bcc1fc0SBob Badour
34*9bcc1fc0SBob Badour /* Encode the root device structuring here for Chromium OS */
35*9bcc1fc0SBob Badour static const char kActiveRoot[] = "/dev/ACTIVE_ROOT";
36*9bcc1fc0SBob Badour static const char kRootDev[] = "/dev/ROOT";
37*9bcc1fc0SBob Badour static const char kRootA[] = "/dev/ROOT0";
38*9bcc1fc0SBob Badour static const char kRootB[] = "/dev/ROOT1";
39*9bcc1fc0SBob Badour
40*9bcc1fc0SBob Badour struct part_config {
41*9bcc1fc0SBob Badour const char *name;
42*9bcc1fc0SBob Badour int offset;
43*9bcc1fc0SBob Badour };
44*9bcc1fc0SBob Badour
45*9bcc1fc0SBob Badour #define CHROMEOS_PRIMARY_PARTITION 3
46*9bcc1fc0SBob Badour static const struct part_config kPrimaryPart[] = { { kRootA, 0 },
47*9bcc1fc0SBob Badour { kRootDev, -3 },
48*9bcc1fc0SBob Badour { kRootB, 2 } };
49*9bcc1fc0SBob Badour #define CHROMEOS_SECONDARY_PARTITION 5
50*9bcc1fc0SBob Badour static const struct part_config kSecondaryPart[] = { { kRootB, 0 },
51*9bcc1fc0SBob Badour { kRootDev, -5 },
52*9bcc1fc0SBob Badour { kRootA, -2 } };
53*9bcc1fc0SBob Badour
54*9bcc1fc0SBob Badour /* The number of entries in a part_config so we could add RootC easily. */
55*9bcc1fc0SBob Badour static const int kPartitionEntries = 3;
56*9bcc1fc0SBob Badour
57*9bcc1fc0SBob Badour /* Converts a file of %u:%u -> dev_t. */
devt_from_file(const char * file)58*9bcc1fc0SBob Badour static dev_t devt_from_file(const char *file) {
59*9bcc1fc0SBob Badour char candidate[10]; /* TODO(wad) system-provided constant? */
60*9bcc1fc0SBob Badour ssize_t bytes = 0;
61*9bcc1fc0SBob Badour unsigned int major_num = 0;
62*9bcc1fc0SBob Badour unsigned int minor_num = 0;
63*9bcc1fc0SBob Badour dev_t dev = 0;
64*9bcc1fc0SBob Badour int fd = -1;
65*9bcc1fc0SBob Badour
66*9bcc1fc0SBob Badour /* Never hang. Either get the data or return 0. */
67*9bcc1fc0SBob Badour fd = open(file, O_NONBLOCK | O_RDONLY);
68*9bcc1fc0SBob Badour if (fd < 0)
69*9bcc1fc0SBob Badour return 0;
70*9bcc1fc0SBob Badour bytes = read(fd, candidate, sizeof(candidate));
71*9bcc1fc0SBob Badour close(fd);
72*9bcc1fc0SBob Badour
73*9bcc1fc0SBob Badour /* 0:0 should be considered the minimum size. */
74*9bcc1fc0SBob Badour if (bytes < 3)
75*9bcc1fc0SBob Badour return 0;
76*9bcc1fc0SBob Badour candidate[bytes] = 0;
77*9bcc1fc0SBob Badour if (sscanf(candidate, "%u:%u", &major_num, &minor_num) == 2) {
78*9bcc1fc0SBob Badour /* candidate's size artificially limits the size of the converted
79*9bcc1fc0SBob Badour * %u to safely convert to a signed int. */
80*9bcc1fc0SBob Badour dev = makedev(major_num, minor_num);
81*9bcc1fc0SBob Badour }
82*9bcc1fc0SBob Badour return dev;
83*9bcc1fc0SBob Badour }
84*9bcc1fc0SBob Badour
85*9bcc1fc0SBob Badour /* Walks sysfs and recurses into any directory/link that represents
86*9bcc1fc0SBob Badour * a block device to find sub-devices (partitions) for dev.
87*9bcc1fc0SBob Badour * If dev == 0, the name fo the first device in the directory will be returned.
88*9bcc1fc0SBob Badour * Returns the device's name in "name" */
match_sysfs_device(char * name,size_t name_len,const char * basedir,dev_t * dev,int depth)89*9bcc1fc0SBob Badour static int match_sysfs_device(char *name, size_t name_len,
90*9bcc1fc0SBob Badour const char *basedir, dev_t *dev, int depth) {
91*9bcc1fc0SBob Badour int found = -1;
92*9bcc1fc0SBob Badour size_t basedir_len;
93*9bcc1fc0SBob Badour DIR *dirp = NULL;
94*9bcc1fc0SBob Badour struct dirent *entry = NULL;
95*9bcc1fc0SBob Badour struct dirent *next = NULL;
96*9bcc1fc0SBob Badour char *working_path = NULL;
97*9bcc1fc0SBob Badour long working_path_size = 0;
98*9bcc1fc0SBob Badour
99*9bcc1fc0SBob Badour if (!name || !name_len || !basedir || !dev) {
100*9bcc1fc0SBob Badour warnx("match_sysfs_device: invalid arguments supplied");
101*9bcc1fc0SBob Badour return -1;
102*9bcc1fc0SBob Badour }
103*9bcc1fc0SBob Badour basedir_len = strlen(basedir);
104*9bcc1fc0SBob Badour if (!basedir_len) {
105*9bcc1fc0SBob Badour warnx("match_sysfs_device: basedir must not be empty");
106*9bcc1fc0SBob Badour return -1;
107*9bcc1fc0SBob Badour }
108*9bcc1fc0SBob Badour
109*9bcc1fc0SBob Badour errno = 0;
110*9bcc1fc0SBob Badour dirp = opendir(basedir);
111*9bcc1fc0SBob Badour if (!dirp) {
112*9bcc1fc0SBob Badour /* Don't complain if the directory doesn't exist. */
113*9bcc1fc0SBob Badour if (errno != ENOENT)
114*9bcc1fc0SBob Badour warn("match_sysfs_device:opendir(%s)", basedir);
115*9bcc1fc0SBob Badour return found;
116*9bcc1fc0SBob Badour }
117*9bcc1fc0SBob Badour
118*9bcc1fc0SBob Badour /* Grab a platform appropriate path to work with.
119*9bcc1fc0SBob Badour * Ideally, this won't vary under sys/block. */
120*9bcc1fc0SBob Badour working_path_size = pathconf(basedir, _PC_NAME_MAX) + 1;
121*9bcc1fc0SBob Badour /* Fallback to PATH_MAX on any pathconf error. */
122*9bcc1fc0SBob Badour if (working_path_size < 0)
123*9bcc1fc0SBob Badour working_path_size = PATH_MAX;
124*9bcc1fc0SBob Badour
125*9bcc1fc0SBob Badour working_path = malloc(working_path_size);
126*9bcc1fc0SBob Badour if (!working_path) {
127*9bcc1fc0SBob Badour warn("malloc(dirent)");
128*9bcc1fc0SBob Badour closedir(dirp);
129*9bcc1fc0SBob Badour return found;
130*9bcc1fc0SBob Badour }
131*9bcc1fc0SBob Badour
132*9bcc1fc0SBob Badour /* Allocate a properly sized entry. */
133*9bcc1fc0SBob Badour entry = malloc(offsetof(struct dirent, d_name) + working_path_size);
134*9bcc1fc0SBob Badour if (!entry) {
135*9bcc1fc0SBob Badour warn("malloc(dirent)");
136*9bcc1fc0SBob Badour free(working_path);
137*9bcc1fc0SBob Badour closedir(dirp);
138*9bcc1fc0SBob Badour return found;
139*9bcc1fc0SBob Badour }
140*9bcc1fc0SBob Badour
141*9bcc1fc0SBob Badour while (readdir_r(dirp, entry, &next) == 0 && next) {
142*9bcc1fc0SBob Badour size_t candidate_len = strlen(entry->d_name);
143*9bcc1fc0SBob Badour size_t path_len = 0;
144*9bcc1fc0SBob Badour dev_t found_devt = 0;
145*9bcc1fc0SBob Badour /* Ignore the usual */
146*9bcc1fc0SBob Badour if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
147*9bcc1fc0SBob Badour continue;
148*9bcc1fc0SBob Badour /* TODO(wad) determine how to best bubble up this case. */
149*9bcc1fc0SBob Badour if (candidate_len > name_len)
150*9bcc1fc0SBob Badour continue;
151*9bcc1fc0SBob Badour /* Only traverse directories or symlinks (to directories ideally) */
152*9bcc1fc0SBob Badour switch (entry->d_type) {
153*9bcc1fc0SBob Badour case DT_UNKNOWN:
154*9bcc1fc0SBob Badour case DT_DIR:
155*9bcc1fc0SBob Badour case DT_LNK:
156*9bcc1fc0SBob Badour break;
157*9bcc1fc0SBob Badour default:
158*9bcc1fc0SBob Badour continue;
159*9bcc1fc0SBob Badour }
160*9bcc1fc0SBob Badour /* Determine path to block device number */
161*9bcc1fc0SBob Badour path_len = snprintf(working_path, working_path_size, "%s/%s/dev",
162*9bcc1fc0SBob Badour basedir, entry->d_name);
163*9bcc1fc0SBob Badour /* Ignore if truncation occurs. */
164*9bcc1fc0SBob Badour if (path_len != candidate_len + basedir_len + 5)
165*9bcc1fc0SBob Badour continue;
166*9bcc1fc0SBob Badour
167*9bcc1fc0SBob Badour found_devt = devt_from_file(working_path);
168*9bcc1fc0SBob Badour /* *dev == 0 is a wildcard. */
169*9bcc1fc0SBob Badour if (!*dev || found_devt == *dev) {
170*9bcc1fc0SBob Badour snprintf(name, name_len, "%s", entry->d_name);
171*9bcc1fc0SBob Badour *dev = found_devt;
172*9bcc1fc0SBob Badour found = 1;
173*9bcc1fc0SBob Badour break;
174*9bcc1fc0SBob Badour }
175*9bcc1fc0SBob Badour
176*9bcc1fc0SBob Badour /* Prevent infinite recursion on symlink loops by limiting depth. */
177*9bcc1fc0SBob Badour if (depth > 5)
178*9bcc1fc0SBob Badour break;
179*9bcc1fc0SBob Badour
180*9bcc1fc0SBob Badour /* Recurse one level for devices that may have a matching partition. */
181*9bcc1fc0SBob Badour if (major(found_devt) == major(*dev) && minor(*dev) > minor(found_devt)) {
182*9bcc1fc0SBob Badour sprintf(working_path, "%s/%s", basedir, entry->d_name);
183*9bcc1fc0SBob Badour found = match_sysfs_device(name, name_len, working_path, dev, depth + 1);
184*9bcc1fc0SBob Badour if (found > 0)
185*9bcc1fc0SBob Badour break;
186*9bcc1fc0SBob Badour }
187*9bcc1fc0SBob Badour }
188*9bcc1fc0SBob Badour
189*9bcc1fc0SBob Badour free(working_path);
190*9bcc1fc0SBob Badour free(entry);
191*9bcc1fc0SBob Badour closedir(dirp);
192*9bcc1fc0SBob Badour return found;
193*9bcc1fc0SBob Badour }
194*9bcc1fc0SBob Badour
rootdev_get_partition(const char * dst,size_t len)195*9bcc1fc0SBob Badour const char *rootdev_get_partition(const char *dst, size_t len) {
196*9bcc1fc0SBob Badour const char *end = dst + strnlen(dst, len);
197*9bcc1fc0SBob Badour const char *part = end - 1;
198*9bcc1fc0SBob Badour if (!len)
199*9bcc1fc0SBob Badour return NULL;
200*9bcc1fc0SBob Badour
201*9bcc1fc0SBob Badour if (!isdigit(*part--))
202*9bcc1fc0SBob Badour return NULL;
203*9bcc1fc0SBob Badour
204*9bcc1fc0SBob Badour while (part > dst && isdigit(*part)) part--;
205*9bcc1fc0SBob Badour part++;
206*9bcc1fc0SBob Badour
207*9bcc1fc0SBob Badour if (part >= end)
208*9bcc1fc0SBob Badour return NULL;
209*9bcc1fc0SBob Badour
210*9bcc1fc0SBob Badour return part;
211*9bcc1fc0SBob Badour }
212*9bcc1fc0SBob Badour
rootdev_strip_partition(char * dst,size_t len)213*9bcc1fc0SBob Badour void rootdev_strip_partition(char *dst, size_t len) {
214*9bcc1fc0SBob Badour char *part = (char *)rootdev_get_partition(dst, len);
215*9bcc1fc0SBob Badour if (!part)
216*9bcc1fc0SBob Badour return;
217*9bcc1fc0SBob Badour /* For devices that end with a digit, the kernel uses a 'p'
218*9bcc1fc0SBob Badour * as a separator. E.g., mmcblk1p2. */
219*9bcc1fc0SBob Badour if (*(part - 1) == 'p')
220*9bcc1fc0SBob Badour part--;
221*9bcc1fc0SBob Badour *part = '\0';
222*9bcc1fc0SBob Badour }
223*9bcc1fc0SBob Badour
rootdev_symlink_active(const char * path)224*9bcc1fc0SBob Badour int rootdev_symlink_active(const char *path) {
225*9bcc1fc0SBob Badour int ret = 0;
226*9bcc1fc0SBob Badour /* Don't overwrite an existing link. */
227*9bcc1fc0SBob Badour errno = 0;
228*9bcc1fc0SBob Badour if ((symlink(path, kActiveRoot)) && errno != EEXIST) {
229*9bcc1fc0SBob Badour warn("failed to symlink %s -> %s", kActiveRoot, path);
230*9bcc1fc0SBob Badour ret = -1;
231*9bcc1fc0SBob Badour }
232*9bcc1fc0SBob Badour return ret;
233*9bcc1fc0SBob Badour }
234*9bcc1fc0SBob Badour
rootdev_get_device(char * dst,size_t size,dev_t dev,const char * search)235*9bcc1fc0SBob Badour int rootdev_get_device(char *dst, size_t size, dev_t dev,
236*9bcc1fc0SBob Badour const char *search) {
237*9bcc1fc0SBob Badour struct stat active_root_statbuf;
238*9bcc1fc0SBob Badour
239*9bcc1fc0SBob Badour if (search == NULL)
240*9bcc1fc0SBob Badour search = kDefaultSearchPath;
241*9bcc1fc0SBob Badour
242*9bcc1fc0SBob Badour /* Check if the -s symlink exists. */
243*9bcc1fc0SBob Badour if ((stat(kActiveRoot, &active_root_statbuf) == 0) &&
244*9bcc1fc0SBob Badour active_root_statbuf.st_rdev == dev) {
245*9bcc1fc0SBob Badour /* Note, if the link is not fully qualified, this won't be
246*9bcc1fc0SBob Badour * either. */
247*9bcc1fc0SBob Badour ssize_t len = readlink(kActiveRoot, dst, PATH_MAX);
248*9bcc1fc0SBob Badour if (len > 0) {
249*9bcc1fc0SBob Badour dst[len] = 0;
250*9bcc1fc0SBob Badour return 0;
251*9bcc1fc0SBob Badour }
252*9bcc1fc0SBob Badour /* If readlink fails or is empty, fall through */
253*9bcc1fc0SBob Badour }
254*9bcc1fc0SBob Badour
255*9bcc1fc0SBob Badour snprintf(dst, size, "%s", search);
256*9bcc1fc0SBob Badour if (match_sysfs_device(dst, size, dst, &dev, 0) <= 0) {
257*9bcc1fc0SBob Badour fprintf (stderr, "unable to find match\n");
258*9bcc1fc0SBob Badour return 1;
259*9bcc1fc0SBob Badour }
260*9bcc1fc0SBob Badour
261*9bcc1fc0SBob Badour return 0;
262*9bcc1fc0SBob Badour }
263*9bcc1fc0SBob Badour
264*9bcc1fc0SBob Badour /*
265*9bcc1fc0SBob Badour * rootdev_get_device_slave returns results in slave which
266*9bcc1fc0SBob Badour * may be the original device or the name of the slave.
267*9bcc1fc0SBob Badour *
268*9bcc1fc0SBob Badour * Because slave and device may point to the same data,
269*9bcc1fc0SBob Badour * must be careful how they are handled because slave
270*9bcc1fc0SBob Badour * is modified (can't use snprintf).
271*9bcc1fc0SBob Badour */
rootdev_get_device_slave(char * slave,size_t size,dev_t * dev,const char * device,const char * search)272*9bcc1fc0SBob Badour void rootdev_get_device_slave(char *slave, size_t size, dev_t *dev,
273*9bcc1fc0SBob Badour const char *device, const char *search) {
274*9bcc1fc0SBob Badour char dst[PATH_MAX];
275*9bcc1fc0SBob Badour int len = 0;
276*9bcc1fc0SBob Badour int i;
277*9bcc1fc0SBob Badour
278*9bcc1fc0SBob Badour if (search == NULL)
279*9bcc1fc0SBob Badour search = kDefaultSearchPath;
280*9bcc1fc0SBob Badour
281*9bcc1fc0SBob Badour /*
282*9bcc1fc0SBob Badour * With stacked device mappers, we have to chain through all the levels
283*9bcc1fc0SBob Badour * and find the last device. For example, verity can be stacked on bootcache
284*9bcc1fc0SBob Badour * that is stacked on a disk partition.
285*9bcc1fc0SBob Badour */
286*9bcc1fc0SBob Badour if (slave != device)
287*9bcc1fc0SBob Badour strncpy(slave, device, size);
288*9bcc1fc0SBob Badour slave[size - 1] = '\0';
289*9bcc1fc0SBob Badour for (i = 0; i < MAX_SLAVE_DEPTH; i++) {
290*9bcc1fc0SBob Badour len = snprintf(dst, sizeof(dst), "%s/%s/slaves", search, slave);
291*9bcc1fc0SBob Badour if (len != strlen(device) + strlen(search) + 8) {
292*9bcc1fc0SBob Badour warnx("rootdev_get_device_slave: device name too long");
293*9bcc1fc0SBob Badour return;
294*9bcc1fc0SBob Badour }
295*9bcc1fc0SBob Badour *dev = 0;
296*9bcc1fc0SBob Badour if (match_sysfs_device(slave, size, dst, dev, 0) <= 0) {
297*9bcc1fc0SBob Badour return;
298*9bcc1fc0SBob Badour }
299*9bcc1fc0SBob Badour }
300*9bcc1fc0SBob Badour warnx("slave depth greater than %d at %s", i, slave);
301*9bcc1fc0SBob Badour }
302*9bcc1fc0SBob Badour
rootdev_create_devices(const char * name,dev_t dev,bool symlink)303*9bcc1fc0SBob Badour int rootdev_create_devices(const char *name, dev_t dev, bool symlink) {
304*9bcc1fc0SBob Badour int ret = 0;
305*9bcc1fc0SBob Badour unsigned int major_num = major(dev);
306*9bcc1fc0SBob Badour unsigned int minor_num = minor(dev);
307*9bcc1fc0SBob Badour int i;
308*9bcc1fc0SBob Badour const struct part_config *config;
309*9bcc1fc0SBob Badour const char *part_s = rootdev_get_partition(name, strlen(name));
310*9bcc1fc0SBob Badour
311*9bcc1fc0SBob Badour if (part_s == NULL) {
312*9bcc1fc0SBob Badour warnx("create_devices: unable to determine partition");
313*9bcc1fc0SBob Badour return -1;
314*9bcc1fc0SBob Badour }
315*9bcc1fc0SBob Badour
316*9bcc1fc0SBob Badour switch (atoi(part_s)) {
317*9bcc1fc0SBob Badour case CHROMEOS_PRIMARY_PARTITION:
318*9bcc1fc0SBob Badour config = kPrimaryPart;
319*9bcc1fc0SBob Badour break;
320*9bcc1fc0SBob Badour case CHROMEOS_SECONDARY_PARTITION:
321*9bcc1fc0SBob Badour config = kSecondaryPart;
322*9bcc1fc0SBob Badour break;
323*9bcc1fc0SBob Badour default:
324*9bcc1fc0SBob Badour warnx("create_devices: unable to determine partition: %s",
325*9bcc1fc0SBob Badour part_s);
326*9bcc1fc0SBob Badour return -1;
327*9bcc1fc0SBob Badour }
328*9bcc1fc0SBob Badour
329*9bcc1fc0SBob Badour for (i = 0; i < kPartitionEntries; ++i) {
330*9bcc1fc0SBob Badour dev = makedev(major_num, minor_num + config[i].offset);
331*9bcc1fc0SBob Badour errno = 0;
332*9bcc1fc0SBob Badour if (mknod(config[i].name,
333*9bcc1fc0SBob Badour S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH,
334*9bcc1fc0SBob Badour dev) && errno != EEXIST) {
335*9bcc1fc0SBob Badour warn("failed to create %s", config[i].name);
336*9bcc1fc0SBob Badour return -1;
337*9bcc1fc0SBob Badour }
338*9bcc1fc0SBob Badour }
339*9bcc1fc0SBob Badour
340*9bcc1fc0SBob Badour if (symlink)
341*9bcc1fc0SBob Badour ret = rootdev_symlink_active(config[0].name);
342*9bcc1fc0SBob Badour return ret;
343*9bcc1fc0SBob Badour }
344*9bcc1fc0SBob Badour
rootdev_get_path(char * path,size_t size,const char * device,const char * dev_path)345*9bcc1fc0SBob Badour int rootdev_get_path(char *path, size_t size, const char *device,
346*9bcc1fc0SBob Badour const char *dev_path) {
347*9bcc1fc0SBob Badour int path_len;
348*9bcc1fc0SBob Badour
349*9bcc1fc0SBob Badour if (!dev_path)
350*9bcc1fc0SBob Badour dev_path = kDefaultDevPath;
351*9bcc1fc0SBob Badour
352*9bcc1fc0SBob Badour if (!path || !size || !device)
353*9bcc1fc0SBob Badour return -1;
354*9bcc1fc0SBob Badour
355*9bcc1fc0SBob Badour path_len = snprintf(path, size, "%s/%s", dev_path, device);
356*9bcc1fc0SBob Badour if (path_len != strlen(dev_path) + 1 + strlen(device))
357*9bcc1fc0SBob Badour return -1;
358*9bcc1fc0SBob Badour
359*9bcc1fc0SBob Badour // TODO(bsimonnet): We should check that |path| exists and is the right
360*9bcc1fc0SBob Badour // device. We don't do this currently as OEMs can add custom SELinux rules
361*9bcc1fc0SBob Badour // which may prevent us from accessing this.
362*9bcc1fc0SBob Badour // See b/24267261.
363*9bcc1fc0SBob Badour
364*9bcc1fc0SBob Badour return 0;
365*9bcc1fc0SBob Badour }
366*9bcc1fc0SBob Badour
rootdev_wrapper(char * path,size_t size,bool full,bool strip,dev_t * dev,const char * search,const char * dev_path)367*9bcc1fc0SBob Badour int rootdev_wrapper(char *path, size_t size,
368*9bcc1fc0SBob Badour bool full, bool strip,
369*9bcc1fc0SBob Badour dev_t *dev,
370*9bcc1fc0SBob Badour const char *search, const char *dev_path) {
371*9bcc1fc0SBob Badour int res = 0;
372*9bcc1fc0SBob Badour char devname[PATH_MAX];
373*9bcc1fc0SBob Badour if (!search)
374*9bcc1fc0SBob Badour search = kDefaultSearchPath;
375*9bcc1fc0SBob Badour if (!dev_path)
376*9bcc1fc0SBob Badour dev_path = kDefaultDevPath;
377*9bcc1fc0SBob Badour if (!dev)
378*9bcc1fc0SBob Badour return -1;
379*9bcc1fc0SBob Badour
380*9bcc1fc0SBob Badour res = rootdev_get_device(devname, sizeof(devname), *dev, search);
381*9bcc1fc0SBob Badour if (res != 0)
382*9bcc1fc0SBob Badour return res;
383*9bcc1fc0SBob Badour
384*9bcc1fc0SBob Badour if (full)
385*9bcc1fc0SBob Badour rootdev_get_device_slave(devname, sizeof(devname), dev, devname,
386*9bcc1fc0SBob Badour search);
387*9bcc1fc0SBob Badour
388*9bcc1fc0SBob Badour /* TODO(wad) we should really just track the block dev, partition number, and
389*9bcc1fc0SBob Badour * dev path. When we rewrite this, we can track all the sysfs info
390*9bcc1fc0SBob Badour * in the class. */
391*9bcc1fc0SBob Badour if (strip) {
392*9bcc1fc0SBob Badour /* When we strip the partition, we don't want get_path to return non-zero
393*9bcc1fc0SBob Badour * because of dev mismatch. Passing in 0 tells it to not test. */
394*9bcc1fc0SBob Badour *dev = 0;
395*9bcc1fc0SBob Badour rootdev_strip_partition(devname, size);
396*9bcc1fc0SBob Badour }
397*9bcc1fc0SBob Badour
398*9bcc1fc0SBob Badour res = rootdev_get_path(path, size, devname, dev_path);
399*9bcc1fc0SBob Badour
400*9bcc1fc0SBob Badour return res;
401*9bcc1fc0SBob Badour }
402*9bcc1fc0SBob Badour
rootdev(char * path,size_t size,bool full,bool strip)403*9bcc1fc0SBob Badour int rootdev(char *path, size_t size, bool full, bool strip) {
404*9bcc1fc0SBob Badour struct stat root_statbuf;
405*9bcc1fc0SBob Badour dev_t _root_dev, *root_dev = &_root_dev;
406*9bcc1fc0SBob Badour
407*9bcc1fc0SBob Badour /* Yields the containing dev_t in st_dev. */
408*9bcc1fc0SBob Badour if (stat("/data", &root_statbuf) != 0)
409*9bcc1fc0SBob Badour return -1;
410*9bcc1fc0SBob Badour
411*9bcc1fc0SBob Badour /* Some ABIs (like mips o32) are broken and the st_dev field isn't actually
412*9bcc1fc0SBob Badour * a dev_t. In that case, pass a pointer to a local dev_t who we took care
413*9bcc1fc0SBob Badour * of truncating the value into. On sane arches, gcc can optimize this to
414*9bcc1fc0SBob Badour * the same code, so should only be a penalty when the ABI is broken. */
415*9bcc1fc0SBob Badour if (sizeof(root_statbuf.st_dev) == sizeof(*root_dev)) {
416*9bcc1fc0SBob Badour /* Cast is OK since we verified size here. */
417*9bcc1fc0SBob Badour root_dev = (dev_t *)&root_statbuf.st_dev;
418*9bcc1fc0SBob Badour } else {
419*9bcc1fc0SBob Badour *root_dev = root_statbuf.st_dev;
420*9bcc1fc0SBob Badour }
421*9bcc1fc0SBob Badour
422*9bcc1fc0SBob Badour return rootdev_wrapper(path,
423*9bcc1fc0SBob Badour size,
424*9bcc1fc0SBob Badour full,
425*9bcc1fc0SBob Badour strip,
426*9bcc1fc0SBob Badour root_dev,
427*9bcc1fc0SBob Badour NULL, /* default /sys dir */
428*9bcc1fc0SBob Badour NULL); /* default /dev dir */
429*9bcc1fc0SBob Badour }
430