/src/barebox/common/bootscan.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | #define pr_fmt(fmt) "bootscan: " fmt |
3 | | |
4 | | #include <driver.h> |
5 | | #include <xfuncs.h> |
6 | | #include <block.h> |
7 | | #include <disks.h> |
8 | | #include <fs.h> |
9 | | #include <linux/stat.h> |
10 | | #include <linux/err.h> |
11 | | #include <linux/minmax.h> |
12 | | #include <mtd/ubi-user.h> |
13 | | #include <uapi/spec/dps.h> |
14 | | |
15 | | #include <bootscan.h> |
16 | | |
17 | | static int boot_scan_device(struct bootscanner *scanner, |
18 | | struct bootentries *bootentries, struct device *dev) |
19 | 0 | { |
20 | 0 | struct cdev *cdev; |
21 | 0 | int ret; |
22 | |
|
23 | 0 | pr_debug("%s(%s): %s\n", __func__, scanner->name, dev_name(dev)); |
24 | |
|
25 | 0 | device_detect(dev); |
26 | |
|
27 | 0 | if (!scanner->scan_disk) |
28 | 0 | goto scan_device; |
29 | | |
30 | 0 | list_for_each_entry(cdev, &dev->cdevs, devices_list) { |
31 | 0 | if (cdev_is_partition(cdev)) |
32 | 0 | continue; |
33 | | |
34 | 0 | ret = scanner->scan_disk(scanner, bootentries, cdev); |
35 | 0 | if (ret) |
36 | 0 | return ret; |
37 | 0 | } |
38 | | |
39 | 0 | scan_device: |
40 | 0 | return scanner->scan_device(scanner, bootentries, dev); |
41 | 0 | } |
42 | | |
43 | | /* |
44 | | * boot_scan_ubi - scan over a cdev containing UBI volumes |
45 | | * |
46 | | * This function attaches a cdev as UBI devices and collects all bootentries |
47 | | * entries found in the UBI volumes |
48 | | * |
49 | | * returns the number of entries found or a negative error code if some unexpected |
50 | | * error occurred. |
51 | | */ |
52 | | static int boot_scan_ubi(struct bootscanner *scanner, |
53 | | struct bootentries *bootentries, struct cdev *cdev) |
54 | 0 | { |
55 | 0 | struct device *child; |
56 | 0 | int ret, found = 0; |
57 | |
|
58 | 0 | pr_debug("%s(%s): %s\n", __func__, scanner->name, cdev->name); |
59 | |
|
60 | 0 | if (!scanner->scan_disk && !scanner->scan_device) |
61 | 0 | return 0; |
62 | | |
63 | 0 | ret = ubi_attach_mtd_dev(cdev->mtd, UBI_DEV_NUM_AUTO, 0, 20); |
64 | 0 | if (ret && ret != -EEXIST) |
65 | 0 | return 0; |
66 | | |
67 | 0 | device_for_each_child(cdev->dev, child) { |
68 | 0 | ret = boot_scan_device(scanner, bootentries, child); |
69 | 0 | if (ret > 0) |
70 | 0 | found += ret; |
71 | 0 | } |
72 | |
|
73 | 0 | return found; |
74 | 0 | } |
75 | | |
76 | | /* |
77 | | * boot_scan_cdev - scan over a cdev |
78 | | * |
79 | | * Given a cdev this function mounts the filesystem and collects all bootentries |
80 | | * entries found in it. |
81 | | * |
82 | | * returns the number of entries found or a negative error code if some unexpected |
83 | | * error occurred. |
84 | | */ |
85 | | int boot_scan_cdev(struct bootscanner *scanner, |
86 | | struct bootentries *bootentries, struct cdev *cdev, |
87 | | bool autodiscover) |
88 | 0 | { |
89 | 0 | int ret, found = 0; |
90 | 0 | unsigned int blocksize; |
91 | 0 | size_t bufsize, readsize; |
92 | 0 | void *buf; |
93 | 0 | enum filetype type, filetype; |
94 | 0 | const char *rootpath; |
95 | |
|
96 | 0 | pr_debug("%s(%s): %s\n", __func__, scanner->name, cdev->name); |
97 | |
|
98 | 0 | if (autodiscover && (cdev->typeflags & DPS_TYPE_FLAG_NO_AUTO)) { |
99 | 0 | pr_debug("auto discovery skipped\n"); |
100 | 0 | return 0; |
101 | 0 | } |
102 | | |
103 | 0 | blocksize = cdev_blocksize(cdev); |
104 | 0 | bufsize = max_t(size_t, FILE_TYPE_SAFE_BUFSIZE, 2 * blocksize); |
105 | |
|
106 | 0 | buf = xzalloc(bufsize); |
107 | |
|
108 | 0 | ret = cdev_read(cdev, buf, bufsize, 0, 0); |
109 | 0 | if (ret < 0) { |
110 | 0 | free(buf); |
111 | 0 | return ret; |
112 | 0 | } |
113 | | |
114 | 0 | readsize = ret; |
115 | |
|
116 | 0 | type = file_detect_partition_table(buf, readsize, blocksize); |
117 | 0 | filetype = file_detect_type(buf, readsize); |
118 | 0 | free(buf); |
119 | |
|
120 | 0 | if (type == filetype_mbr || type == filetype_gpt) { |
121 | 0 | if (!scanner->scan_disk || cdev_is_partition(cdev)) |
122 | 0 | return -EINVAL; |
123 | 0 | return scanner->scan_disk(scanner, bootentries, cdev); |
124 | 0 | } |
125 | | |
126 | 0 | if (filetype == filetype_ubi && IS_ENABLED(CONFIG_MTD_UBI)) { |
127 | 0 | ret = boot_scan_ubi(scanner, bootentries, cdev); |
128 | 0 | if (ret > 0) |
129 | 0 | found += ret; |
130 | 0 | } |
131 | |
|
132 | 0 | rootpath = cdev_mount(cdev); |
133 | 0 | if (!IS_ERR(rootpath) && scanner->scan_directory) { |
134 | 0 | ret = scanner->scan_directory(scanner, bootentries, rootpath); |
135 | 0 | if (ret > 0) |
136 | 0 | found += ret; |
137 | 0 | } |
138 | |
|
139 | 0 | return found; |
140 | 0 | } |
141 | | |
142 | | /* |
143 | | * boot_scan_devicename - scan a hardware device for child cdevs |
144 | | * |
145 | | * Given a name of a hardware device this functions scans over all child |
146 | | * cdevs looking for bootentries entries. |
147 | | * Returns the number of entries found or a negative error code if some unexpected |
148 | | * error occurred. |
149 | | */ |
150 | | static int boot_scan_devicename(struct bootscanner *scanner, |
151 | | struct bootentries *bootentries, const char *devname) |
152 | 0 | { |
153 | 0 | struct device *dev; |
154 | 0 | struct cdev *cdev; |
155 | |
|
156 | 0 | pr_debug("%s(%s): %s\n", __func__, scanner->name, devname); |
157 | | |
158 | | /* Support both boot /dev/disk0.rootfs and boot disk0.rootfs */ |
159 | 0 | devname = devpath_to_name(devname); |
160 | |
|
161 | 0 | device_detect_by_name(devname); |
162 | |
|
163 | 0 | cdev = cdev_by_name(devname); |
164 | 0 | if (cdev) { |
165 | 0 | int ret = boot_scan_cdev(scanner, bootentries, cdev, false); |
166 | 0 | if (ret > 0) |
167 | 0 | return ret; |
168 | 0 | } |
169 | | |
170 | 0 | if (!scanner->scan_device) |
171 | 0 | return 0; |
172 | | |
173 | 0 | dev = get_device_by_name(devname); |
174 | 0 | if (!dev) |
175 | 0 | return -ENODEV; |
176 | | |
177 | 0 | return boot_scan_device(scanner, bootentries, dev); |
178 | 0 | } |
179 | | |
180 | | int bootentry_scan_generate(struct bootscanner *scanner, |
181 | | struct bootentries *bootentries, |
182 | | const char *name) |
183 | 0 | { |
184 | 0 | struct stat s; |
185 | 0 | int ret, found = 0; |
186 | |
|
187 | 0 | ret = boot_scan_devicename(scanner, bootentries, name); |
188 | 0 | if (ret > 0) |
189 | 0 | found += ret; |
190 | |
|
191 | 0 | if (*name == '/') { |
192 | 0 | ret = stat(name, &s); |
193 | 0 | if (ret) |
194 | 0 | return found; |
195 | | |
196 | 0 | if (S_ISDIR(s.st_mode) && scanner->scan_directory) |
197 | 0 | ret = scanner->scan_directory(scanner, bootentries, name); |
198 | 0 | else if (S_ISREG(s.st_mode) && scanner->scan_file) |
199 | 0 | ret = scanner->scan_file(scanner, bootentries, name); |
200 | 0 | if (ret > 0) |
201 | 0 | found += ret; |
202 | 0 | } |
203 | | |
204 | 0 | return found; |
205 | 0 | } |