blob: 87361d2b92349c248da0693aae0fd5ec147f8030 [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 * Interface for root device discovery via sysfs with optional
6 * bells and whistles.
7 */
8#ifndef ROOTDEV_ROOTDEV_H_
9#define ROOTDEV_ROOTDEV_H_
10
11#include <stdbool.h>
12#include <sys/types.h>
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
18/**
19 * rootdev: returns the path to the root device in @path
20 * @path: pre-allocated char array the result will be written to
21 * @size: size of @path
22 * @full: whether to try to do full resolution. E.g., device-mapper
23 * @strip: whether to remove the partition # or not.
24 *
25 * Returns 0 on success, non-zero on error.
26 */
27int rootdev(char *path, size_t size, bool full, bool strip);
28
29/* All interface below this point will most definitely be C specific. If
30 * we rewrite this as a C++ class, only the above generic interface should
31 * still be provided.
32 */
33
34/**
35 * rootdev_wrapper: rootdev equivalent with paths can be substituted.
36 */
37int rootdev_wrapper(char *path, size_t size,
38 bool full, bool strip,
39 dev_t *dev,
Xiaochu Liub9b8f1d2017-04-24 13:32:06 -070040 const char *mount_path,
Will Drewry80fbc6c2010-08-30 10:13:34 -050041 const char *search, const char *dev_path);
42/**
43 * rootdev_get_device: finds the /dev path for @dev
44 * @dst: destination char array
45 * @size: size of @dst
46 * @dev: dev_t specifying the known root device
47 * @search: path to search under. NULL for default.
48 *
49 * Returns 0 on success, non-zero on error.
50 *
51 * The name of the devices is placed in @dst. It will not
52 * be qualified with /dev/ by default.
53 */
54int rootdev_get_device(char *dst, size_t size, dev_t dev,
55 const char *search);
56
57/**
58 * rootdev_get_device_slave: returns the first device under @device/slaves
59 * @slave: destination char array for storing the result
60 * @size: size of @slave
61 * @dev: pointer to a dev_t to populate
62 * @device: name of the device to probe, like "sdb"
63 * @search: path to search under. NULL for default.
64 *
Will Drewry80fbc6c2010-08-30 10:13:34 -050065 * It is safe for @device == @slave.
66 */
Paul Taysomc9880282012-08-30 08:33:32 -070067void rootdev_get_device_slave(char *slave, size_t size, dev_t *dev,
68 const char *device, const char *search);
Will Drewry80fbc6c2010-08-30 10:13:34 -050069
70/**
71 * rootdev_get_path: converts a device name to a path in the device tree
72 * @path: char array to store the path
73 * @size: size of @devpath
74 * @device: name of the device
75 * @dev: optional expected dev_t of the node.
76 * @dev_path: path to dev tree. NULL for default (/dev)
77 *
78 * A @dev of 0 is ignored.
79 *
80 * @path is populated for all return codes.
81 * Returns 0 on success and non-zero on error:
82 * -1 on unexpected errors (@path may be invalid)
83 * 1 on no existing @path
84 * 2 @path exists but the dev_t value is mismatched.
85 *
86 * Nb, this function does NOT search /dev for a match. It performs a normal
87 * string concatenation and probes for the existence. If udev has moved,
88 * or otherwise renamed, the device, a positive value is returned.
89 * The caller may then use the dev_t and @path to create the node with
90 * mknod(2).
91 */
92int rootdev_get_path(char *path, size_t size, const char *device, dev_t dev,
93 const char *dev_path);
94
95const char *rootdev_get_partition(const char *dst, size_t len);
96void rootdev_strip_partition(char *dst, size_t len);
97int rootdev_symlink_active(const char *path);
98int rootdev_create_devices(const char *name, dev_t dev, bool symlink);
99
100#ifdef __cplusplus
101} /* extern "C" */
102#endif
103
104#endif /* ROOTDEV_ROOTDEV_H_ */