blob: 3162be997947c73425bb82d453ba3d88ace8a007 [file] [log] [blame]
Will Drewry80fbc6c2010-08-30 10:13:34 -05001/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Implements root device discovery via sysfs with optional bells and whistles.
Bill Richardson75fcf622010-03-16 13:05:12 -07006 */
7
Will Drewry80fbc6c2010-08-30 10:13:34 -05008#include "rootdev.h"
9
10#include <ctype.h>
Bill Richardson75fcf622010-03-16 13:05:12 -070011#include <dirent.h>
Will Drewry80fbc6c2010-08-30 10:13:34 -050012#include <err.h>
13#include <errno.h>
14#include <fcntl.h>
15#include <stdbool.h>
16#include <stddef.h>
17#include <stdio.h>
18#include <stdlib.h>
Bill Richardson75fcf622010-03-16 13:05:12 -070019#include <string.h>
Will Drewry80fbc6c2010-08-30 10:13:34 -050020#include <sys/stat.h>
21#include <sys/types.h>
22#include <unistd.h>
Bill Richardson75fcf622010-03-16 13:05:12 -070023
Will Drewry80fbc6c2010-08-30 10:13:34 -050024static const char *kDefaultSearchPath = "/sys/block";
25static const char *kDefaultDevPath = "/dev";
Bill Richardson75fcf622010-03-16 13:05:12 -070026
Will Drewry80fbc6c2010-08-30 10:13:34 -050027/* Encode the root device structuring here for Chromium OS */
28static const char kActiveRoot[] = "/dev/ACTIVE_ROOT";
29static const char kRootDev[] = "/dev/ROOT";
30static const char kRootA[] = "/dev/ROOT0";
31static const char kRootB[] = "/dev/ROOT1";
Bill Richardson75fcf622010-03-16 13:05:12 -070032
Will Drewry80fbc6c2010-08-30 10:13:34 -050033struct part_config {
34 const char *name;
35 int offset;
36};
37
38#define CHROMEOS_PRIMARY_PARTITION 3
39static const struct part_config kPrimaryPart[] = { { kRootA, 0 },
40 { kRootDev, -3 },
41 { kRootB, 2 } };
42#define CHROMEOS_SECONDARY_PARTITION 5
43static const struct part_config kSecondaryPart[] = { { kRootB, 0 },
44 { kRootDev, -5 },
45 { kRootA, -2 } };
46
47/* The number of entries in a part_config so we could add RootC easily. */
48static const int kPartitionEntries = 3;
49
50/* Converts a file of %u:%u -> dev_t. */
51static dev_t devt_from_file(const char *file) {
52 char candidate[10]; /* TODO(wad) system-provided constant? */
53 ssize_t bytes = 0;
54 unsigned int major = 0;
55 unsigned int minor = 0;
56 dev_t dev = 0;
57 int fd = -1;
58
59 /* Never hang. Either get the data or return 0. */
60 fd = open(file, O_NONBLOCK | O_RDONLY);
61 if (fd < 0)
62 return 0;
63 bytes = read(fd, candidate, sizeof(candidate));
64 close(fd);
65
66 /* 0:0 should be considered the minimum size. */
67 if (bytes < 3)
68 return 0;
69 candidate[bytes] = 0;
70 if (sscanf(candidate, "%u:%u", &major, &minor) == 2) {
71 /* candidate's size artificially limits the size of the converted
72 * %u to safely convert to a signed int. */
73 dev = makedev(major, minor);
74 }
75 return dev;
Bill Richardson75fcf622010-03-16 13:05:12 -070076}
77
Will Drewry80fbc6c2010-08-30 10:13:34 -050078/* Walks sysfs and will recurse into any directory/link that represents
79 * a block device to find sub-devices (partitions).
80 * If dev == 0, the first device in the directory will be returned. */
81static int match_sysfs_device(char *name, size_t name_len,
Bryan Freedf8e5f9f2011-11-11 13:05:30 -080082 const char *basedir, dev_t *dev, int depth) {
Will Drewry80fbc6c2010-08-30 10:13:34 -050083 int found = -1;
84 size_t basedir_len;
85 DIR *dirp = NULL;
86 struct dirent *entry = NULL;
87 struct dirent *next = NULL;
88 char *working_path = NULL;
89 long working_path_size = 0;
90
91 if (!name || !name_len || !basedir || !dev) {
92 warnx("match_sysfs_device: invalid arguments supplied");
93 return -1;
94 }
95 basedir_len = strlen(basedir);
96 if (!basedir_len) {
97 warnx("match_sysfs_device: basedir must not be empty");
98 return -1;
99 }
100
101 errno = 0;
102 dirp = opendir(basedir);
103 if (!dirp) {
104 /* Don't complain if the directory doesn't exist. */
105 if (errno != ENOENT)
106 warn("match_sysfs_device:opendir(%s)", basedir);
107 return found;
108 }
109
110 /* Grab a platform appropriate path to work with.
111 * Ideally, this won't vary under sys/block. */
112 working_path_size = pathconf(basedir, _PC_NAME_MAX) + 1;
113 /* Fallback to PATH_MAX on any pathconf error. */
114 if (working_path_size < 0)
115 working_path_size = PATH_MAX;
116
117 working_path = malloc(working_path_size);
118 if (!working_path) {
119 warn("malloc(dirent)");
120 closedir(dirp);
121 return found;
122 }
123
124 /* Allocate a properly sized entry. */
125 entry = malloc(offsetof(struct dirent, d_name) + working_path_size);
126 if (!entry) {
127 warn("malloc(dirent)");
128 free(working_path);
129 closedir(dirp);
130 return found;
131 }
132
133 while (readdir_r(dirp, entry, &next) == 0 && next) {
134 size_t candidate_len = strlen(entry->d_name);
135 size_t path_len = 0;
136 dev_t found_devt = 0;
137 /* Ignore the usual */
138 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
139 continue;
140 /* TODO(wad) determine how to best bubble up this case. */
141 if (candidate_len > name_len)
142 continue;
143 /* Only traverse directories or symlinks (to directories ideally) */
144 switch (entry->d_type) {
145 case DT_UNKNOWN:
146 case DT_DIR:
147 case DT_LNK:
148 break;
149 default:
150 continue;
151 }
152 /* Determine path to block device number */
153 path_len = snprintf(working_path, working_path_size, "%s/%s/dev",
154 basedir, entry->d_name);
155 /* Ignore if truncation occurs. */
156 if (path_len != candidate_len + basedir_len + 5)
157 continue;
158
159 found_devt = devt_from_file(working_path);
160 /* *dev == 0 is a wildcard. */
161 if (!*dev || found_devt == *dev) {
162 snprintf(name, name_len, "%s", entry->d_name);
163 *dev = found_devt;
164 found = 1;
165 break;
166 }
167
Bryan Freedf8e5f9f2011-11-11 13:05:30 -0800168 /* Prevent infinite recursion on symlink loops by limiting depth. */
169 if (depth > 5)
170 break;
171
Will Drewry80fbc6c2010-08-30 10:13:34 -0500172 /* Recurse one level for devices that may have a matching partition. */
173 if (major(found_devt) == major(*dev) && minor(*dev) > minor(found_devt)) {
174 sprintf(working_path, "%s/%s", basedir, entry->d_name);
Bryan Freedf8e5f9f2011-11-11 13:05:30 -0800175 found = match_sysfs_device(name, name_len, working_path, dev, depth + 1);
Will Drewry80fbc6c2010-08-30 10:13:34 -0500176 if (found > 0)
177 break;
178 }
179 }
180
181 free(working_path);
182 free(entry);
183 closedir(dirp);
184 return found;
Kobi Cohen-Araziafb2fb52010-07-26 12:48:36 -0600185}
186
Will Drewry80fbc6c2010-08-30 10:13:34 -0500187const char *rootdev_get_partition(const char *dst, size_t len) {
188 const char *end = dst + strnlen(dst, len);
189 const char *part = end - 1;
190 if (!len)
191 return NULL;
Bill Richardson75fcf622010-03-16 13:05:12 -0700192
Will Drewry80fbc6c2010-08-30 10:13:34 -0500193 if (!isdigit(*part--))
194 return NULL;
Kobi Cohen-Araziafb2fb52010-07-26 12:48:36 -0600195
Will Drewry80fbc6c2010-08-30 10:13:34 -0500196 while (part > dst && isdigit(*part)) part--;
197 part++;
Bill Richardson75fcf622010-03-16 13:05:12 -0700198
Will Drewry80fbc6c2010-08-30 10:13:34 -0500199 if (part >= end)
200 return NULL;
Bill Richardson75fcf622010-03-16 13:05:12 -0700201
Will Drewry80fbc6c2010-08-30 10:13:34 -0500202 return part;
203}
Bill Richardson75fcf622010-03-16 13:05:12 -0700204
Will Drewry80fbc6c2010-08-30 10:13:34 -0500205void rootdev_strip_partition(char *dst, size_t len) {
206 char *part = (char *)rootdev_get_partition(dst, len);
207 if (!part)
208 return;
209 /* For devices that end with a digit, the kernel uses a 'p'
210 * as a separator. E.g., mmcblk1p2. */
211 if (*(part - 1) == 'p')
212 part--;
213 *part = '\0';
214}
Kobi Cohen-Araziafb2fb52010-07-26 12:48:36 -0600215
Will Drewry80fbc6c2010-08-30 10:13:34 -0500216int rootdev_symlink_active(const char *path) {
217 int ret = 0;
218 /* Don't overwrite an existing link. */
219 errno = 0;
220 if ((symlink(path, kActiveRoot)) && errno != EEXIST) {
221 warn("failed to symlink %s -> %s", kActiveRoot, path);
222 ret = -1;
223 }
224 return ret;
225}
226
227int rootdev_get_device(char *dst, size_t size, dev_t dev,
228 const char *search) {
229 struct stat active_root_statbuf;
230
231 if (search == NULL)
232 search = kDefaultSearchPath;
233
234 /* Check if the -s symlink exists. */
235 if ((stat(kActiveRoot, &active_root_statbuf) == 0) &&
236 active_root_statbuf.st_rdev == dev) {
237 /* Note, if the link is not fully qualified, this won't be
238 * either. */
239 ssize_t len = readlink(kActiveRoot, dst, PATH_MAX);
240 if (len > 0) {
241 dst[len] = 0;
242 return 0;
243 }
244 /* If readlink fails or is empty, fall through */
245 }
246
247 snprintf(dst, size, "%s", search);
Bryan Freedf8e5f9f2011-11-11 13:05:30 -0800248 if (match_sysfs_device(dst, size, dst, &dev, 0) <= 0) {
Will Drewry80fbc6c2010-08-30 10:13:34 -0500249 fprintf (stderr, "unable to find match\n");
250 return 1;
251 }
252
253 return 0;
254}
255
256int rootdev_get_device_slave(char *slave, size_t size, dev_t *dev,
257 const char *device, const char *search) {
258 char dst[PATH_MAX];
259 int len = 0;
260
261 if (search == NULL)
262 search = kDefaultSearchPath;
263
264 /* So far, I've only seen top-level block devices with slaves. */
265 len = snprintf(dst, sizeof(dst), "%s/%s/slaves", search, device);
266 if (len < 0 || len != strlen(device) + strlen(search) + 8) {
267 warnx("rootdev_get_device_slave: device name too long");
268 return -1;
269 }
270 *dev = 0;
Bryan Freedf8e5f9f2011-11-11 13:05:30 -0800271 if (match_sysfs_device(slave, size, dst, dev, 0) <= 0)
Will Drewry80fbc6c2010-08-30 10:13:34 -0500272 return -1;
273
274 return 0;
275}
276
277int rootdev_create_devices(const char *name, dev_t dev, bool symlink) {
278 int ret = 0;
279 unsigned int major = major(dev);
280 unsigned int minor = minor(dev);
281 int i;
282 const struct part_config *config;
283 const char *part_s = rootdev_get_partition(name, strlen(name));
284
285 if (part_s == NULL) {
286 warnx("create_devices: unable to determine partition");
287 return -1;
288 }
289
290 switch (atoi(part_s)) {
291 case CHROMEOS_PRIMARY_PARTITION:
292 config = kPrimaryPart;
293 break;
294 case CHROMEOS_SECONDARY_PARTITION:
295 config = kSecondaryPart;
296 break;
297 default:
298 warnx("create_devices: unable to determine partition: %s",
299 part_s);
300 return -1;
301 }
302
303 for (i = 0; i < kPartitionEntries; ++i) {
304 dev = makedev(major, minor + config[i].offset);
305 errno = 0;
306 if (mknod(config[i].name,
307 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH,
308 dev) && errno != EEXIST) {
309 warn("failed to create %s", config[i].name);
310 return -1;
311 }
312 }
313
314 if (symlink)
315 ret = rootdev_symlink_active(config[0].name);
316 return ret;
317}
318
319int rootdev_get_path(char *path, size_t size, const char *device,
320 dev_t dev, const char *dev_path) {
321 int path_len;
322 struct stat dev_statbuf;
323
324 if (!dev_path)
325 dev_path = kDefaultDevPath;
326
327 if (!path || !size || !device)
328 return -1;
329
330 path_len = snprintf(path, size, "%s/%s", dev_path, device);
331 if (path_len != strlen(dev_path) + 1 + strlen(device))
332 return -1;
333
334 if (stat(path, &dev_statbuf) != 0)
335 return 1;
336
337 if (dev && dev != dev_statbuf.st_rdev)
338 return 2;
339
340 return 0;
341}
342
343int rootdev_wrapper(char *path, size_t size,
344 bool full, bool strip,
345 dev_t *dev,
346 const char *search, const char *dev_path) {
347 int res = 0;
348 char devname[PATH_MAX];
349 if (!search)
350 search = kDefaultSearchPath;
351 if (!dev_path)
352 dev_path = kDefaultDevPath;
353 if (!dev)
354 return -1;
355
356 res = rootdev_get_device(devname, sizeof(devname), *dev, search);
357 if (res != 0)
358 return res;
359
360 if (full)
361 res = rootdev_get_device_slave(devname, sizeof(devname), dev, devname,
362 search);
363
364 /* TODO(wad) we should really just track the block dev, partition number, and
365 * dev path. When we rewrite this, we can track all the sysfs info
366 * in the class. */
367 if (strip) {
368 /* When we strip the partition, we don't want get_path to return non-zero
369 * because of dev mismatch. Passing in 0 tells it to not test. */
370 *dev = 0;
371 rootdev_strip_partition(devname, size);
372 }
373
374 res = rootdev_get_path(path, size, devname, *dev, dev_path);
375
376 return res;
377}
378
379int rootdev(char *path, size_t size, bool full, bool strip) {
380 struct stat root_statbuf;
381
382 /* Yields the containing dev_t in st_dev. */
383 if (stat("/", &root_statbuf) != 0)
384 return -1;
385
386 return rootdev_wrapper(path,
387 size,
388 full,
389 strip,
390 &root_statbuf.st_dev,
391 NULL, /* default /sys dir */
392 NULL); /* default /dev dir */
Bill Richardson75fcf622010-03-16 13:05:12 -0700393}