xref: /aosp_15_r20/external/rootdev/rootdev.h (revision 9bcc1fc0ac6cc0ff33916f8e6ce540d24c01bac6)
1*9bcc1fc0SBob Badour /* Copyright (c) 2010 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  * Interface for root device discovery via sysfs with optional
6*9bcc1fc0SBob Badour  * bells and whistles.
7*9bcc1fc0SBob Badour  */
8*9bcc1fc0SBob Badour #ifndef ROOTDEV_ROOTDEV_H_
9*9bcc1fc0SBob Badour #define ROOTDEV_ROOTDEV_H_
10*9bcc1fc0SBob Badour 
11*9bcc1fc0SBob Badour #include <stdbool.h>
12*9bcc1fc0SBob Badour #include <sys/types.h>
13*9bcc1fc0SBob Badour 
14*9bcc1fc0SBob Badour #ifdef __cplusplus
15*9bcc1fc0SBob Badour extern "C" {
16*9bcc1fc0SBob Badour #endif
17*9bcc1fc0SBob Badour 
18*9bcc1fc0SBob Badour /**
19*9bcc1fc0SBob Badour  * rootdev: returns the path to the root device in @path
20*9bcc1fc0SBob Badour  * @path: pre-allocated char array the result will be written to
21*9bcc1fc0SBob Badour  * @size: size of @path
22*9bcc1fc0SBob Badour  * @full: whether to try to do full resolution. E.g., device-mapper
23*9bcc1fc0SBob Badour  * @strip: whether to remove the partition # or not.
24*9bcc1fc0SBob Badour  *
25*9bcc1fc0SBob Badour  * Returns 0 on success, non-zero on error.
26*9bcc1fc0SBob Badour  */
27*9bcc1fc0SBob Badour int rootdev(char *path, size_t size, bool full, bool strip);
28*9bcc1fc0SBob Badour 
29*9bcc1fc0SBob Badour /* All interface below this point will most definitely be C specific. If
30*9bcc1fc0SBob Badour  * we rewrite this as a C++ class, only the above generic interface should
31*9bcc1fc0SBob Badour  * still be provided.
32*9bcc1fc0SBob Badour  */
33*9bcc1fc0SBob Badour 
34*9bcc1fc0SBob Badour /**
35*9bcc1fc0SBob Badour  * rootdev_wrapper: rootdev equivalent with paths can be substituted.
36*9bcc1fc0SBob Badour  */
37*9bcc1fc0SBob Badour int rootdev_wrapper(char *path, size_t size,
38*9bcc1fc0SBob Badour                     bool full, bool strip,
39*9bcc1fc0SBob Badour                     dev_t *dev,
40*9bcc1fc0SBob Badour                     const char *search, const char *dev_path);
41*9bcc1fc0SBob Badour /**
42*9bcc1fc0SBob Badour  * rootdev_get_device: finds the /dev path for @dev
43*9bcc1fc0SBob Badour  * @dst: destination char array
44*9bcc1fc0SBob Badour  * @size: size of @dst
45*9bcc1fc0SBob Badour  * @dev: dev_t specifying the known root device
46*9bcc1fc0SBob Badour  * @search: path to search under. NULL for default.
47*9bcc1fc0SBob Badour  *
48*9bcc1fc0SBob Badour  * Returns 0 on success, non-zero on error.
49*9bcc1fc0SBob Badour  *
50*9bcc1fc0SBob Badour  * The name of the devices is placed in @dst. It will not
51*9bcc1fc0SBob Badour  * be qualified with /dev/ by default.
52*9bcc1fc0SBob Badour  */
53*9bcc1fc0SBob Badour int rootdev_get_device(char *dst, size_t size, dev_t dev,
54*9bcc1fc0SBob Badour                        const char *search);
55*9bcc1fc0SBob Badour 
56*9bcc1fc0SBob Badour /**
57*9bcc1fc0SBob Badour  * rootdev_get_device_slave: returns the first device under @device/slaves
58*9bcc1fc0SBob Badour  * @slave: destination char array for storing the result
59*9bcc1fc0SBob Badour  * @size: size of @slave
60*9bcc1fc0SBob Badour  * @dev: pointer to a dev_t to populate
61*9bcc1fc0SBob Badour  * @device: name of the device to probe, like "sdb"
62*9bcc1fc0SBob Badour  * @search: path to search under. NULL for default.
63*9bcc1fc0SBob Badour  *
64*9bcc1fc0SBob Badour  * It is safe for @device == @slave.
65*9bcc1fc0SBob Badour  */
66*9bcc1fc0SBob Badour void rootdev_get_device_slave(char *slave, size_t size, dev_t *dev,
67*9bcc1fc0SBob Badour                               const char *device, const char *search);
68*9bcc1fc0SBob Badour 
69*9bcc1fc0SBob Badour /**
70*9bcc1fc0SBob Badour  * rootdev_get_path: converts a device name to a path in the device tree
71*9bcc1fc0SBob Badour  * @path: char array to store the path
72*9bcc1fc0SBob Badour  * @size: size of @devpath
73*9bcc1fc0SBob Badour  * @device: name of the device
74*9bcc1fc0SBob Badour  * @dev_path: path to dev tree. NULL for default (/dev)
75*9bcc1fc0SBob Badour  *
76*9bcc1fc0SBob Badour  * A @dev of 0 is ignored.
77*9bcc1fc0SBob Badour  *
78*9bcc1fc0SBob Badour  * @path is populated for all return codes.
79*9bcc1fc0SBob Badour  * Returns 0 on success and non-zero on error:
80*9bcc1fc0SBob Badour  * -1 on unexpected errors (@path may be invalid)
81*9bcc1fc0SBob Badour  *
82*9bcc1fc0SBob Badour  * Nb, this function does NOT search /dev for a match.  It performs a normal
83*9bcc1fc0SBob Badour  *     string concatenation.
84*9bcc1fc0SBob Badour  *     We can't check if the device actually exists as vendors may create an
85*9bcc1fc0SBob Badour  *     SELinux context we don't know about for it (in which case, this function
86*9bcc1fc0SBob Badour  *     would always fail).
87*9bcc1fc0SBob Badour  */
88*9bcc1fc0SBob Badour int rootdev_get_path(char *path, size_t size, const char *device,
89*9bcc1fc0SBob Badour                      const char *dev_path);
90*9bcc1fc0SBob Badour 
91*9bcc1fc0SBob Badour const char *rootdev_get_partition(const char *dst, size_t len);
92*9bcc1fc0SBob Badour void rootdev_strip_partition(char *dst, size_t len);
93*9bcc1fc0SBob Badour int rootdev_symlink_active(const char *path);
94*9bcc1fc0SBob Badour int rootdev_create_devices(const char *name, dev_t dev, bool symlink);
95*9bcc1fc0SBob Badour 
96*9bcc1fc0SBob Badour #ifdef __cplusplus
97*9bcc1fc0SBob Badour }  /* extern "C" */
98*9bcc1fc0SBob Badour #endif
99*9bcc1fc0SBob Badour 
100*9bcc1fc0SBob Badour #endif  /* ROOTDEV_ROOTDEV_H_ */
101