blob: 867974f50cb13f6b24033f635a9607a0b2510bad [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,
82 const char *basedir, dev_t *dev) {
83 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
168 /* Recurse one level for devices that may have a matching partition. */
169 if (major(found_devt) == major(*dev) && minor(*dev) > minor(found_devt)) {
170 sprintf(working_path, "%s/%s", basedir, entry->d_name);
171 found = match_sysfs_device(name, name_len, working_path, dev);
172 if (found > 0)
173 break;
174 }
175 }
176
177 free(working_path);
178 free(entry);
179 closedir(dirp);
180 return found;
Kobi Cohen-Araziafb2fb52010-07-26 12:48:36 -0600181}
182
Will Drewry80fbc6c2010-08-30 10:13:34 -0500183const char *rootdev_get_partition(const char *dst, size_t len) {
184 const char *end = dst + strnlen(dst, len);
185 const char *part = end - 1;
186 if (!len)
187 return NULL;
Bill Richardson75fcf622010-03-16 13:05:12 -0700188
Will Drewry80fbc6c2010-08-30 10:13:34 -0500189 if (!isdigit(*part--))
190 return NULL;
Kobi Cohen-Araziafb2fb52010-07-26 12:48:36 -0600191
Will Drewry80fbc6c2010-08-30 10:13:34 -0500192 while (part > dst && isdigit(*part)) part--;
193 part++;
Bill Richardson75fcf622010-03-16 13:05:12 -0700194
Will Drewry80fbc6c2010-08-30 10:13:34 -0500195 if (part >= end)
196 return NULL;
Bill Richardson75fcf622010-03-16 13:05:12 -0700197
Will Drewry80fbc6c2010-08-30 10:13:34 -0500198 return part;
199}
Bill Richardson75fcf622010-03-16 13:05:12 -0700200
Will Drewry80fbc6c2010-08-30 10:13:34 -0500201void rootdev_strip_partition(char *dst, size_t len) {
202 char *part = (char *)rootdev_get_partition(dst, len);
203 if (!part)
204 return;
205 /* For devices that end with a digit, the kernel uses a 'p'
206 * as a separator. E.g., mmcblk1p2. */
207 if (*(part - 1) == 'p')
208 part--;
209 *part = '\0';
210}
Kobi Cohen-Araziafb2fb52010-07-26 12:48:36 -0600211
Will Drewry80fbc6c2010-08-30 10:13:34 -0500212int rootdev_symlink_active(const char *path) {
213 int ret = 0;
214 /* Don't overwrite an existing link. */
215 errno = 0;
216 if ((symlink(path, kActiveRoot)) && errno != EEXIST) {
217 warn("failed to symlink %s -> %s", kActiveRoot, path);
218 ret = -1;
219 }
220 return ret;
221}
222
223int rootdev_get_device(char *dst, size_t size, dev_t dev,
224 const char *search) {
225 struct stat active_root_statbuf;
226
227 if (search == NULL)
228 search = kDefaultSearchPath;
229
230 /* Check if the -s symlink exists. */
231 if ((stat(kActiveRoot, &active_root_statbuf) == 0) &&
232 active_root_statbuf.st_rdev == dev) {
233 /* Note, if the link is not fully qualified, this won't be
234 * either. */
235 ssize_t len = readlink(kActiveRoot, dst, PATH_MAX);
236 if (len > 0) {
237 dst[len] = 0;
238 return 0;
239 }
240 /* If readlink fails or is empty, fall through */
241 }
242
243 snprintf(dst, size, "%s", search);
244 if (match_sysfs_device(dst, size, dst, &dev) <= 0) {
245 fprintf (stderr, "unable to find match\n");
246 return 1;
247 }
248
249 return 0;
250}
251
252int rootdev_get_device_slave(char *slave, size_t size, dev_t *dev,
253 const char *device, const char *search) {
254 char dst[PATH_MAX];
255 int len = 0;
256
257 if (search == NULL)
258 search = kDefaultSearchPath;
259
260 /* So far, I've only seen top-level block devices with slaves. */
261 len = snprintf(dst, sizeof(dst), "%s/%s/slaves", search, device);
262 if (len < 0 || len != strlen(device) + strlen(search) + 8) {
263 warnx("rootdev_get_device_slave: device name too long");
264 return -1;
265 }
266 *dev = 0;
267 if (match_sysfs_device(slave, size, dst, dev) <= 0)
268 return -1;
269
270 return 0;
271}
272
273int rootdev_create_devices(const char *name, dev_t dev, bool symlink) {
274 int ret = 0;
275 unsigned int major = major(dev);
276 unsigned int minor = minor(dev);
277 int i;
278 const struct part_config *config;
279 const char *part_s = rootdev_get_partition(name, strlen(name));
280
281 if (part_s == NULL) {
282 warnx("create_devices: unable to determine partition");
283 return -1;
284 }
285
286 switch (atoi(part_s)) {
287 case CHROMEOS_PRIMARY_PARTITION:
288 config = kPrimaryPart;
289 break;
290 case CHROMEOS_SECONDARY_PARTITION:
291 config = kSecondaryPart;
292 break;
293 default:
294 warnx("create_devices: unable to determine partition: %s",
295 part_s);
296 return -1;
297 }
298
299 for (i = 0; i < kPartitionEntries; ++i) {
300 dev = makedev(major, minor + config[i].offset);
301 errno = 0;
302 if (mknod(config[i].name,
303 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH,
304 dev) && errno != EEXIST) {
305 warn("failed to create %s", config[i].name);
306 return -1;
307 }
308 }
309
310 if (symlink)
311 ret = rootdev_symlink_active(config[0].name);
312 return ret;
313}
314
315int rootdev_get_path(char *path, size_t size, const char *device,
316 dev_t dev, const char *dev_path) {
317 int path_len;
318 struct stat dev_statbuf;
319
320 if (!dev_path)
321 dev_path = kDefaultDevPath;
322
323 if (!path || !size || !device)
324 return -1;
325
326 path_len = snprintf(path, size, "%s/%s", dev_path, device);
327 if (path_len != strlen(dev_path) + 1 + strlen(device))
328 return -1;
329
330 if (stat(path, &dev_statbuf) != 0)
331 return 1;
332
333 if (dev && dev != dev_statbuf.st_rdev)
334 return 2;
335
336 return 0;
337}
338
339int rootdev_wrapper(char *path, size_t size,
340 bool full, bool strip,
341 dev_t *dev,
342 const char *search, const char *dev_path) {
343 int res = 0;
344 char devname[PATH_MAX];
345 if (!search)
346 search = kDefaultSearchPath;
347 if (!dev_path)
348 dev_path = kDefaultDevPath;
349 if (!dev)
350 return -1;
351
352 res = rootdev_get_device(devname, sizeof(devname), *dev, search);
353 if (res != 0)
354 return res;
355
356 if (full)
357 res = rootdev_get_device_slave(devname, sizeof(devname), dev, devname,
358 search);
359
360 /* TODO(wad) we should really just track the block dev, partition number, and
361 * dev path. When we rewrite this, we can track all the sysfs info
362 * in the class. */
363 if (strip) {
364 /* When we strip the partition, we don't want get_path to return non-zero
365 * because of dev mismatch. Passing in 0 tells it to not test. */
366 *dev = 0;
367 rootdev_strip_partition(devname, size);
368 }
369
370 res = rootdev_get_path(path, size, devname, *dev, dev_path);
371
372 return res;
373}
374
375int rootdev(char *path, size_t size, bool full, bool strip) {
376 struct stat root_statbuf;
377
378 /* Yields the containing dev_t in st_dev. */
379 if (stat("/", &root_statbuf) != 0)
380 return -1;
381
382 return rootdev_wrapper(path,
383 size,
384 full,
385 strip,
386 &root_statbuf.st_dev,
387 NULL, /* default /sys dir */
388 NULL); /* default /dev dir */
Bill Richardson75fcf622010-03-16 13:05:12 -0700389}