evboards.c revision 1.2.2.2 1 1.2.2.2 christos /* $NetBSD: evboards.c,v 1.2.2.2 2019/06/10 22:10:30 christos Exp $ */
2 1.2.2.2 christos
3 1.2.2.2 christos /*-
4 1.2.2.2 christos * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 1.2.2.2 christos * All rights reserved.
6 1.2.2.2 christos *
7 1.2.2.2 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.2.2.2 christos * by Jason R. Thorpe.
9 1.2.2.2 christos *
10 1.2.2.2 christos * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 christos * modification, are permitted provided that the following conditions
12 1.2.2.2 christos * are met:
13 1.2.2.2 christos * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 christos * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 christos * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 christos * documentation and/or other materials provided with the distribution.
18 1.2.2.2 christos *
19 1.2.2.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.2.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.2.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.2.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.2.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.2.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.2.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.2.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.2.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.2.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.2.2 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.2.2.2 christos */
31 1.2.2.2 christos
32 1.2.2.2 christos #if HAVE_NBTOOL_CONFIG_H
33 1.2.2.2 christos #include "nbtool_config.h"
34 1.2.2.2 christos #endif
35 1.2.2.2 christos
36 1.2.2.2 christos #include <sys/cdefs.h>
37 1.2.2.2 christos #if !defined(__lint)
38 1.2.2.2 christos __RCSID("$NetBSD: evboards.c,v 1.2.2.2 2019/06/10 22:10:30 christos Exp $");
39 1.2.2.2 christos #endif /* !__lint */
40 1.2.2.2 christos
41 1.2.2.2 christos #include <sys/types.h>
42 1.2.2.2 christos #include <sys/stat.h>
43 1.2.2.2 christos #include <assert.h>
44 1.2.2.2 christos #include <err.h>
45 1.2.2.2 christos #include <errno.h>
46 1.2.2.2 christos #include <fcntl.h>
47 1.2.2.2 christos #include <fts.h>
48 1.2.2.2 christos #include <inttypes.h>
49 1.2.2.2 christos #include <limits.h>
50 1.2.2.2 christos #include <stdarg.h>
51 1.2.2.2 christos #include <stdlib.h>
52 1.2.2.2 christos #include <stdio.h>
53 1.2.2.2 christos #include <string.h>
54 1.2.2.2 christos #include <unistd.h>
55 1.2.2.2 christos
56 1.2.2.2 christos #ifdef SUPPORT_FDT
57 1.2.2.2 christos #include "libfdt.h"
58 1.2.2.2 christos #endif
59 1.2.2.2 christos
60 1.2.2.2 christos #if !HAVE_NBTOOL_CONFIG_H
61 1.2.2.2 christos #include <sys/utsname.h>
62 1.2.2.2 christos
63 1.2.2.2 christos #ifdef SUPPORT_OPENFIRMWARE
64 1.2.2.2 christos #include <sys/ioctl.h>
65 1.2.2.2 christos #include <dev/ofw/openfirmio.h>
66 1.2.2.2 christos #endif
67 1.2.2.2 christos
68 1.2.2.2 christos #endif /* ! HAVE_NBTOOL_CONFIG_H */
69 1.2.2.2 christos
70 1.2.2.2 christos #include "installboot.h"
71 1.2.2.2 christos #include "evboards.h"
72 1.2.2.2 christos
73 1.2.2.2 christos /*
74 1.2.2.2 christos * The board database is implemented as a property list. The base
75 1.2.2.2 christos * system provides a set of known boards, keyed by their "compatible"
76 1.2.2.2 christos * device tree property.
77 1.2.2.2 christos *
78 1.2.2.2 christos * The database provided by the base system is meant to help guide
79 1.2.2.2 christos * the user as to which u-boot package needs to be installed on the
80 1.2.2.2 christos * system in order to write the boot loader to the boot media. The
81 1.2.2.2 christos * base board plist is specific to the $MACHINE (e.g. "evbarm"), and
82 1.2.2.2 christos * is installed along with the build tools, e.g.:
83 1.2.2.2 christos *
84 1.2.2.2 christos * (native location)
85 1.2.2.2 christos * /usr/sbin/installboot
86 1.2.2.2 christos * /usr/share/installboot/evbarm/boards.plist
87 1.2.2.2 christos * /usr/share/installboot/evbmips/boards.plist
88 1.2.2.2 christos *
89 1.2.2.2 christos * (example cross host tool location)
90 1.2.2.2 christos * /usr/local/xnbsd/bin/nbinstallboot
91 1.2.2.2 christos * /usr/local/xnbsd/share/installboot/evbarm/boards.plist
92 1.2.2.2 christos * /usr/local/xnbsd/share/installboot/evbmips/boards.plist
93 1.2.2.2 christos *
94 1.2.2.2 christos * The schema of the base board plist is as follows:
95 1.2.2.2 christos *
96 1.2.2.2 christos * <plist>
97 1.2.2.2 christos * <dict>
98 1.2.2.2 christos * <!--
99 1.2.2.2 christos * -- Key: string matching a "compatible" DT property.
100 1.2.2.2 christos * -- Value: dictionary representing a board object.
101 1.2.2.2 christos * -- (required)
102 1.2.2.2 christos * -->
103 1.2.2.2 christos * <key>example,example-board</key>
104 1.2.2.2 christos * <dict>
105 1.2.2.2 christos * <!--
106 1.2.2.2 christos * -- Key: "description".
107 1.2.2.2 christos * -- Value: string containing the board description.
108 1.2.2.2 christos * -- (required)
109 1.2.2.2 christos * -->
110 1.2.2.2 christos * <key>description</key>
111 1.2.2.2 christos * <string>Example Co. Example Board</string>
112 1.2.2.2 christos *
113 1.2.2.2 christos * <!--
114 1.2.2.2 christos * -- Key: "u-boot-pkg".
115 1.2.2.2 christos * -- Value: string representing the board-specific
116 1.2.2.2 christos * -- portion of the u-boot package name.
117 1.2.2.2 christos * -- In this example, the package's full name
118 1.2.2.2 christos * -- is "u-boot-exampleboard". This is used
119 1.2.2.2 christos * -- to recommend to the user which u-boot
120 1.2.2.2 christos * -- package to install. If not present, then
121 1.2.2.2 christos * -- no package recommendation will be made.
122 1.2.2.2 christos * -- (optional)
123 1.2.2.2 christos * -->
124 1.2.2.2 christos * <key>u-boot-pkg</key>
125 1.2.2.2 christos * <string>exampleboard</string>
126 1.2.2.2 christos * </dict>
127 1.2.2.2 christos * </dict>
128 1.2.2.2 christos * </plist>
129 1.2.2.2 christos *
130 1.2.2.2 christos * Individual u-boot packages install their own overlay property list
131 1.2.2.2 christos * files that installboot(8) then scans for. These overlay files are
132 1.2.2.2 christos * named "installboot.plist", and are installed alongside the u-boot
133 1.2.2.2 christos * binaries by the individual u-boot packages, for example:
134 1.2.2.2 christos *
135 1.2.2.2 christos * /usr/pkg/share/u-boot/exampleboard/installboot.plist
136 1.2.2.2 christos * /usr/pkg/share/u-boot/exampleboard/u-boot-with-spl.bin
137 1.2.2.2 christos *
138 1.2.2.2 christos * installboot(8) scans a set of directories looking for "installboot.plist"
139 1.2.2.2 christos * overlay files one directory deep. For example:
140 1.2.2.2 christos *
141 1.2.2.2 christos * /usr/pkg/share/u-boot/
142 1.2.2.2 christos * exampleboard/installboot.plist
143 1.2.2.2 christos * superarmdeluxe/installboot.plist
144 1.2.2.2 christos * dummy/
145 1.2.2.2 christos *
146 1.2.2.2 christos * In this example, "/usr/pkg/share/u-boot" is scanned, it would identify
147 1.2.2.2 christos * "exampleboard" and "superarmdeluxe" as directories containing overlays
148 1.2.2.2 christos * and load them.
149 1.2.2.2 christos *
150 1.2.2.2 christos * The default path scanned for u-boot packages is:
151 1.2.2.2 christos *
152 1.2.2.2 christos * /usr/pkg/share/u-boot
153 1.2.2.2 christos *
154 1.2.2.2 christos * This can be overridden with the INSTALLBOOT_UBOOT_PATHS environment
155 1.2.2.2 christos * variable, which contains a colon-sparated list of directories, e.g.:
156 1.2.2.2 christos *
157 1.2.2.2 christos * /usr/pkg/share/u-boot:/home/jmcneill/hackityhack/u-boot
158 1.2.2.2 christos *
159 1.2.2.2 christos * The scan only consults the top-level children of the specified directory.
160 1.2.2.2 christos *
161 1.2.2.2 christos * Each overlay includes complete board objects that entirely replace
162 1.2.2.2 christos * the system-provided board objects in memory. Some of the keys in
163 1.2.2.2 christos * overlay board objects are computed at run-time and should not appear
164 1.2.2.2 christos * in the plists loaded from the file system.
165 1.2.2.2 christos *
166 1.2.2.2 christos * The schema of the overlay board plists are as follows:
167 1.2.2.2 christos *
168 1.2.2.2 christos * <plist>
169 1.2.2.2 christos * <dict>
170 1.2.2.2 christos * <!--
171 1.2.2.2 christos * -- Key: string matching a "compatible" DT property.
172 1.2.2.2 christos * -- Value: dictionary representing a board object.
173 1.2.2.2 christos * -- (required)
174 1.2.2.2 christos * -->
175 1.2.2.2 christos * <key>example,example-board</key>
176 1.2.2.2 christos * <dict>
177 1.2.2.2 christos * <!--
178 1.2.2.2 christos * -- Key: "description".
179 1.2.2.2 christos * -- Value: string containing the board description.
180 1.2.2.2 christos * -- (required)
181 1.2.2.2 christos * -->
182 1.2.2.2 christos * <key>description</key>
183 1.2.2.2 christos * <string>Example Co. Example Board</string>
184 1.2.2.2 christos *
185 1.2.2.2 christos * <!--
186 1.2.2.2 christos * -- Key: "u-boot-install".
187 1.2.2.2 christos * -- (and variants; see discussion below)
188 1.2.2.2 christos * -- "u-boot-install-emmc", etc.).
189 1.2.2.2 christos * -- Value: Array of u-boot installation step objects,
190 1.2.2.2 christos * -- as described below.
191 1.2.2.2 christos * -- (required)
192 1.2.2.2 christos * --
193 1.2.2.2 christos * -- At least one of these objects is required. If the
194 1.2.2.2 christos * -- board uses a single set of steps for all boot media
195 1.2.2.2 christos * -- types, then it should provide just "u-boot-install".
196 1.2.2.2 christos * -- Otherwise, it whould provide one or more objects
197 1.2.2.2 christos * -- with names reflecting the media type, e.g.:
198 1.2.2.2 christos * --
199 1.2.2.2 christos * -- "u-boot-install-sdmmc" (for SD cards)
200 1.2.2.2 christos * -- "u-boot-install-emmc" (for eMMC modules)
201 1.2.2.2 christos * -- "u-boot-install-usb" (for USB block storage)
202 1.2.2.2 christos * --
203 1.2.2.2 christos * -- These installation steps will be selectable using
204 1.2.2.2 christos * -- the "media=..." option to installboot(8).
205 1.2.2.2 christos * -->
206 1.2.2.2 christos * <key>u-boot-install</key>
207 1.2.2.2 christos * <array>
208 1.2.2.2 christos * <!-- see installation object discussion below. -->
209 1.2.2.2 christos * </array>
210 1.2.2.2 christos *
211 1.2.2.2 christos * <!--
212 1.2.2.2 christos * -- Key: "runtime-u-boot-path"
213 1.2.2.2 christos * -- Value: A string representing the path to the u-boot
214 1.2.2.2 christos * -- binary files needed to install the boot loader.
215 1.2.2.2 christos * -- This value is computed at run-time and is the
216 1.2.2.2 christos * -- same directory in which the instalboot.plist
217 1.2.2.2 christos * -- file for that u-boot package is located.
218 1.2.2.2 christos * -- This key/value pair should never be included
219 1.2.2.2 christos * -- in an installboot.plist file, and including it
220 1.2.2.2 christos * -- will cause the overlay to be rejected.
221 1.2.2.2 christos * -- (computed at run-time)
222 1.2.2.2 christos * -->
223 1.2.2.2 christos * <key>runtime-u-boot-path</key>
224 1.2.2.2 christos * <string>/usr/pkg/share/u-boot/exampleboard</string>
225 1.2.2.2 christos * </dict>
226 1.2.2.2 christos * </dict>
227 1.2.2.2 christos * </plist>
228 1.2.2.2 christos *
229 1.2.2.2 christos * The installation objects provide a description of the steps needed
230 1.2.2.2 christos * to install u-boot on the boot media. Each installation object it
231 1.2.2.2 christos * itself an array of step object.
232 1.2.2.2 christos *
233 1.2.2.2 christos * A basic installation object has a single step that instructs
234 1.2.2.2 christos * installboot(8) to write a file to a specific offset onto the
235 1.2.2.2 christos * boot media.
236 1.2.2.2 christos *
237 1.2.2.2 christos * <key>u-boot-install</key>
238 1.2.2.2 christos * <!-- installation object -->
239 1.2.2.2 christos * <array>
240 1.2.2.2 christos * <!-- step object -->
241 1.2.2.2 christos * <dict>
242 1.2.2.2 christos * <!--
243 1.2.2.2 christos * -- Key: "file-name".
244 1.2.2.2 christos * -- Value: a string naming the file to be
245 1.2.2.2 christos * -- written to the media.
246 1.2.2.2 christos * -- (required)
247 1.2.2.2 christos * -->
248 1.2.2.2 christos * <key>file-name</key>
249 1.2.2.2 christos * <string>u-boot-with-spl.bin</string>
250 1.2.2.2 christos *
251 1.2.2.2 christos * <!--
252 1.2.2.2 christos * -- Key: "image-offset".
253 1.2.2.2 christos * -- Value: an integer specifying the offset
254 1.2.2.2 christos * -- into the output image or device
255 1.2.2.2 christos * -- where to write the file. Defaults
256 1.2.2.2 christos * -- to 0 if not specified.
257 1.2.2.2 christos * -- (optional)
258 1.2.2.2 christos * -->
259 1.2.2.2 christos * <key>image-offset</key>
260 1.2.2.2 christos * <integer>8192</integer>
261 1.2.2.2 christos * </dict>
262 1.2.2.2 christos * </array>
263 1.2.2.2 christos *
264 1.2.2.2 christos * Some installations require multiple steps with special handling.
265 1.2.2.2 christos *
266 1.2.2.2 christos * <key>u-boot-install</key>
267 1.2.2.2 christos * <array>
268 1.2.2.2 christos * <--
269 1.2.2.2 christos * -- Step 1: Write the initial portion of the boot
270 1.2.2.2 christos * -- loader onto the media. The loader has a "hole"
271 1.2.2.2 christos * -- to leave room for the MBR partition table. Take
272 1.2.2.2 christos * -- care not to scribble over the table.
273 1.2.2.2 christos * -->
274 1.2.2.2 christos * <dict>
275 1.2.2.2 christos * <key>file-name</key>
276 1.2.2.2 christos * <string>u-boot-img.bin</string>
277 1.2.2.2 christos *
278 1.2.2.2 christos * <!--
279 1.2.2.2 christos * -- Key: "file-size".
280 1.2.2.2 christos * -- Value: an integer specifying the amount of
281 1.2.2.2 christos * -- data from the file to be written to the
282 1.2.2.2 christos * -- output. Defaults to "to end of file" if
283 1.2.2.2 christos * -- not specified.
284 1.2.2.2 christos * -- (optional)
285 1.2.2.2 christos * -->
286 1.2.2.2 christos * <!-- Stop short of the MBR partition table. -->
287 1.2.2.2 christos * <key>file-size</key>
288 1.2.2.2 christos * <integer>442</integer>
289 1.2.2.2 christos *
290 1.2.2.2 christos * <!--
291 1.2.2.2 christos * -- Key: "preserve".
292 1.2.2.2 christos * -- Value: a boolean indicating that any partial
293 1.2.2.2 christos * -- output block should preserve any pre-
294 1.2.2.2 christos * -- existing contents of that block for
295 1.2.2.2 christos * -- the portion of the of the block not
296 1.2.2.2 christos * -- overwritten by the input file.
297 1.2.2.2 christos * -- (read-modify-write)
298 1.2.2.2 christos * -- (optional)
299 1.2.2.2 christos * -->
300 1.2.2.2 christos * <!-- Preserve the MBR partition table. -->
301 1.2.2.2 christos * <key>preserve</key>
302 1.2.2.2 christos * <true/>
303 1.2.2.2 christos * </dict>
304 1.2.2.2 christos * <--
305 1.2.2.2 christos * -- Step 2: Write the rest of the loader after the
306 1.2.2.2 christos * -- MBR partition table.
307 1.2.2.2 christos * -->
308 1.2.2.2 christos * <dict>
309 1.2.2.2 christos * <key>file-name</key>
310 1.2.2.2 christos * <string>u-boot-img.bin</string>
311 1.2.2.2 christos *
312 1.2.2.2 christos * <!--
313 1.2.2.2 christos * -- Key: "file-offset".
314 1.2.2.2 christos * -- Value: an integer specifying the offset into
315 1.2.2.2 christos * -- the input file from where to start
316 1.2.2.2 christos * -- copying to the output.
317 1.2.2.2 christos * -- (optional)
318 1.2.2.2 christos * -->
319 1.2.2.2 christos * <key>file-offset</key>
320 1.2.2.2 christos * <integer>512</integer>
321 1.2.2.2 christos *
322 1.2.2.2 christos * <!-- ...just after the MBR partition talble. -->
323 1.2.2.2 christos * <key>image-offset</key>
324 1.2.2.2 christos * <integer>512</integer>
325 1.2.2.2 christos * </dict>
326 1.2.2.2 christos * </array>
327 1.2.2.2 christos */
328 1.2.2.2 christos
329 1.2.2.2 christos /*
330 1.2.2.2 christos * make_path --
331 1.2.2.2 christos * Build a path into the given buffer with the specified
332 1.2.2.2 christos * format. Returns NULL if the path won't fit.
333 1.2.2.2 christos */
334 1.2.2.2 christos static __printflike(3,4) const char *
335 1.2.2.2 christos make_path(char *buf, size_t bufsize, const char *fmt, ...)
336 1.2.2.2 christos {
337 1.2.2.2 christos va_list ap;
338 1.2.2.2 christos int ret;
339 1.2.2.2 christos
340 1.2.2.2 christos va_start(ap, fmt);
341 1.2.2.2 christos ret = vsnprintf(buf, bufsize, fmt, ap);
342 1.2.2.2 christos va_end(ap);
343 1.2.2.2 christos
344 1.2.2.2 christos if (ret < 0 || (size_t)ret >= bufsize)
345 1.2.2.2 christos return NULL;
346 1.2.2.2 christos
347 1.2.2.2 christos return buf;
348 1.2.2.2 christos }
349 1.2.2.2 christos
350 1.2.2.2 christos #ifndef EVBOARDS_PLIST_BASE
351 1.2.2.2 christos #define EVBOARDS_PLIST_BASE "/usr"
352 1.2.2.2 christos #endif
353 1.2.2.2 christos
354 1.2.2.2 christos static const char evb_db_base_location[] =
355 1.2.2.2 christos EVBOARDS_PLIST_BASE "/share/installboot";
356 1.2.2.2 christos
357 1.2.2.2 christos #ifndef DEFAULT_UBOOT_PKG_PATH
358 1.2.2.2 christos #define DEFAULT_UBOOT_PKG_PATH "/usr/pkg/share/u-boot"
359 1.2.2.2 christos #endif
360 1.2.2.2 christos
361 1.2.2.2 christos #ifndef UBOOT_PATHS_ENV_VAR
362 1.2.2.2 christos #define UBOOT_PATHS_ENV_VAR "INSTALLBOOT_UBOOT_PATHS"
363 1.2.2.2 christos #endif
364 1.2.2.2 christos
365 1.2.2.2 christos static const char evb_uboot_pkg_path[] = DEFAULT_UBOOT_PKG_PATH;
366 1.2.2.2 christos
367 1.2.2.2 christos /*
368 1.2.2.2 christos * evb_db_base_path --
369 1.2.2.2 christos * Returns the path to the base board db file.
370 1.2.2.2 christos */
371 1.2.2.2 christos static const char *
372 1.2.2.2 christos evb_db_base_path(ib_params *params, char *buf, size_t bufsize)
373 1.2.2.2 christos {
374 1.2.2.2 christos
375 1.2.2.2 christos return make_path(buf, bufsize, "%s/%s/boards.plist",
376 1.2.2.2 christos evb_db_base_location, params->machine->name);
377 1.2.2.2 christos }
378 1.2.2.2 christos
379 1.2.2.2 christos /*
380 1.2.2.2 christos * evb_uboot_pkg_paths --
381 1.2.2.2 christos * Returns an array of u-boot package paths to scan for
382 1.2.2.2 christos * installboot.plist files.
383 1.2.2.2 christos *
384 1.2.2.2 christos * Number of array elements, not including the NULL terminator,
385 1.2.2.2 christos * is returned in *countp.
386 1.2.2.2 christos *
387 1.2.2.2 christos * The working buffer is returned in *bufp so that the caller
388 1.2.2.2 christos * can free it.
389 1.2.2.2 christos */
390 1.2.2.2 christos static char **
391 1.2.2.2 christos evb_uboot_pkg_paths(ib_params *params, int *countp, void **bufp)
392 1.2.2.2 christos {
393 1.2.2.2 christos char **ret_array = NULL;
394 1.2.2.2 christos char *buf = NULL;
395 1.2.2.2 christos const char *pathspec;
396 1.2.2.2 christos int i, count;
397 1.2.2.2 christos char *cp, *startcp;;
398 1.2.2.2 christos
399 1.2.2.2 christos pathspec = getenv(UBOOT_PATHS_ENV_VAR);
400 1.2.2.2 christos if (pathspec == NULL)
401 1.2.2.2 christos pathspec = evb_uboot_pkg_path;
402 1.2.2.2 christos
403 1.2.2.2 christos if (strlen(pathspec) == 0)
404 1.2.2.2 christos goto out;
405 1.2.2.2 christos
406 1.2.2.2 christos /* Count the path elements. */
407 1.2.2.2 christos for (cp = __UNCONST(pathspec), count = 0;;) {
408 1.2.2.2 christos count++;
409 1.2.2.2 christos cp = strchr(cp, ':');
410 1.2.2.2 christos if (cp == NULL)
411 1.2.2.2 christos break;
412 1.2.2.2 christos cp++;
413 1.2.2.2 christos }
414 1.2.2.2 christos
415 1.2.2.2 christos buf = malloc((sizeof(char *) * (count + 1)) +
416 1.2.2.2 christos strlen(pathspec) + 1);
417 1.2.2.2 christos if (buf == NULL)
418 1.2.2.2 christos goto out;
419 1.2.2.2 christos
420 1.2.2.2 christos /*
421 1.2.2.2 christos * Because we want to follow the usual "paths are listed in priority
422 1.2.2.2 christos * order" semantics, we reverse the order of the paths when we put
423 1.2.2.2 christos * them into the array we feed to fts. This is because we always
424 1.2.2.2 christos * overwrite existing entries as we find them, thus the last board
425 1.2.2.2 christos * object found one a given key is the one that will be used.
426 1.2.2.2 christos */
427 1.2.2.2 christos
428 1.2.2.2 christos ret_array = (char **)buf;
429 1.2.2.2 christos startcp = buf + (sizeof(char *) * (count + 1));
430 1.2.2.2 christos /* this is a safe strcpy(); don't replace it. */
431 1.2.2.2 christos strcpy(startcp, pathspec);
432 1.2.2.2 christos
433 1.2.2.2 christos cp = strrchr(startcp, ':');
434 1.2.2.2 christos if (cp == NULL)
435 1.2.2.2 christos cp = startcp;
436 1.2.2.2 christos
437 1.2.2.2 christos for (i = 0;;) {
438 1.2.2.2 christos if (*cp == ':') {
439 1.2.2.2 christos ret_array[i++] = cp+1;
440 1.2.2.2 christos *cp-- = '\0';
441 1.2.2.2 christos } else
442 1.2.2.2 christos ret_array[i++] = cp;
443 1.2.2.2 christos if (cp == startcp)
444 1.2.2.2 christos break;
445 1.2.2.2 christos cp = strrchr(cp, ':');
446 1.2.2.2 christos if (cp == NULL)
447 1.2.2.2 christos cp = startcp;
448 1.2.2.2 christos }
449 1.2.2.2 christos assert(i == count);
450 1.2.2.2 christos ret_array[i] = NULL;
451 1.2.2.2 christos
452 1.2.2.2 christos out:
453 1.2.2.2 christos if (ret_array == NULL) {
454 1.2.2.2 christos if (buf != NULL)
455 1.2.2.2 christos free(buf);
456 1.2.2.2 christos } else {
457 1.2.2.2 christos if (countp != NULL)
458 1.2.2.2 christos *countp = count;
459 1.2.2.2 christos if (bufp != NULL)
460 1.2.2.2 christos *bufp = buf;
461 1.2.2.2 christos }
462 1.2.2.2 christos return ret_array;
463 1.2.2.2 christos }
464 1.2.2.2 christos
465 1.2.2.2 christos static const char step_file_name_key[] = "file-name";
466 1.2.2.2 christos static const char step_file_offset_key[] = "file-offset";
467 1.2.2.2 christos static const char step_file_size_key[] = "file-size";
468 1.2.2.2 christos static const char step_image_offset_key[] = "image-offset";
469 1.2.2.2 christos static const char step_preserve_key[] = "preserve";
470 1.2.2.2 christos
471 1.2.2.2 christos static bool
472 1.2.2.2 christos validate_ubstep_object(evb_ubstep obj)
473 1.2.2.2 christos {
474 1.2.2.2 christos /*
475 1.2.2.2 christos * evb_ubstep is a dictionary with the following keys:
476 1.2.2.2 christos *
477 1.2.2.2 christos * "file-name" (string) (required)
478 1.2.2.2 christos * "file-offset" (number) (optional)
479 1.2.2.2 christos * "file-size" (number) (optional)
480 1.2.2.2 christos * "image-offset" (number) (optional)
481 1.2.2.2 christos * "preserve" (bool) (optional)
482 1.2.2.2 christos */
483 1.2.2.2 christos if (prop_object_type(obj) != PROP_TYPE_DICTIONARY)
484 1.2.2.2 christos return false;
485 1.2.2.2 christos
486 1.2.2.2 christos prop_object_t v;
487 1.2.2.2 christos
488 1.2.2.2 christos v = prop_dictionary_get(obj, step_file_name_key);
489 1.2.2.2 christos if (v == NULL ||
490 1.2.2.2 christos prop_object_type(v) != PROP_TYPE_STRING)
491 1.2.2.2 christos return false;
492 1.2.2.2 christos
493 1.2.2.2 christos v = prop_dictionary_get(obj, step_file_offset_key);
494 1.2.2.2 christos if (v != NULL &&
495 1.2.2.2 christos prop_object_type(v) != PROP_TYPE_NUMBER)
496 1.2.2.2 christos return false;
497 1.2.2.2 christos
498 1.2.2.2 christos v = prop_dictionary_get(obj, step_file_size_key);
499 1.2.2.2 christos if (v != NULL &&
500 1.2.2.2 christos prop_object_type(v) != PROP_TYPE_NUMBER)
501 1.2.2.2 christos return false;
502 1.2.2.2 christos
503 1.2.2.2 christos v = prop_dictionary_get(obj, step_image_offset_key);
504 1.2.2.2 christos if (v != NULL &&
505 1.2.2.2 christos prop_object_type(v) != PROP_TYPE_NUMBER)
506 1.2.2.2 christos return false;
507 1.2.2.2 christos
508 1.2.2.2 christos v = prop_dictionary_get(obj, step_preserve_key);
509 1.2.2.2 christos if (v != NULL &&
510 1.2.2.2 christos prop_object_type(v) != PROP_TYPE_BOOL)
511 1.2.2.2 christos return false;
512 1.2.2.2 christos
513 1.2.2.2 christos return true;
514 1.2.2.2 christos }
515 1.2.2.2 christos
516 1.2.2.2 christos static bool
517 1.2.2.2 christos validate_ubinstall_object(evb_ubinstall obj)
518 1.2.2.2 christos {
519 1.2.2.2 christos /*
520 1.2.2.2 christos * evb_ubinstall is an array with one or more evb_ubstep
521 1.2.2.2 christos * objects.
522 1.2.2.2 christos *
523 1.2.2.2 christos * (evb_ubsteps is just a convenience type for iterating
524 1.2.2.2 christos * over the steps.)
525 1.2.2.2 christos */
526 1.2.2.2 christos if (prop_object_type(obj) != PROP_TYPE_ARRAY)
527 1.2.2.2 christos return false;
528 1.2.2.2 christos if (prop_array_count(obj) < 1)
529 1.2.2.2 christos return false;
530 1.2.2.2 christos
531 1.2.2.2 christos prop_object_t v;
532 1.2.2.2 christos prop_object_iterator_t iter = prop_array_iterator(obj);
533 1.2.2.2 christos
534 1.2.2.2 christos while ((v = prop_object_iterator_next(iter)) != NULL) {
535 1.2.2.2 christos if (!validate_ubstep_object(v))
536 1.2.2.2 christos break;
537 1.2.2.2 christos }
538 1.2.2.2 christos
539 1.2.2.2 christos prop_object_iterator_release(iter);
540 1.2.2.2 christos return v == NULL;
541 1.2.2.2 christos }
542 1.2.2.2 christos
543 1.2.2.2 christos static const char board_description_key[] = "description";
544 1.2.2.2 christos static const char board_u_boot_pkg_key[] = "u-boot-pkg";
545 1.2.2.2 christos static const char board_u_boot_path_key[] = "runtime-u-boot-path";
546 1.2.2.2 christos static const char board_u_boot_install_key[] = "u-boot-install";
547 1.2.2.2 christos
548 1.2.2.2 christos static bool
549 1.2.2.2 christos validate_board_object(evb_board obj, bool is_overlay)
550 1.2.2.2 christos {
551 1.2.2.2 christos /*
552 1.2.2.2 christos * evb_board is a dictionary with the following keys:
553 1.2.2.2 christos *
554 1.2.2.2 christos * "description" (string) (required)
555 1.2.2.2 christos * "u-boot-pkg" (string) (optional, base only)
556 1.2.2.2 christos * "runtime-u-boot-path" (string) (required, overlay only)
557 1.2.2.2 christos *
558 1.2.2.2 christos * With special consideration for these keys:
559 1.2.2.2 christos *
560 1.2.2.2 christos * Either this key and no other "u-boot-install*" keys:
561 1.2.2.2 christos * "u-boot-install" (string) (required, overlay only)
562 1.2.2.2 christos *
563 1.2.2.2 christos * Or one or more keys of the following pattern:
564 1.2.2.2 christos * "u-boot-install-*" (string) (required, overlay only)
565 1.2.2.2 christos */
566 1.2.2.2 christos bool has_default_install = false;
567 1.2.2.2 christos bool has_media_install = false;
568 1.2.2.2 christos
569 1.2.2.2 christos if (prop_object_type(obj) != PROP_TYPE_DICTIONARY)
570 1.2.2.2 christos return false;
571 1.2.2.2 christos
572 1.2.2.2 christos prop_object_t v;
573 1.2.2.2 christos
574 1.2.2.2 christos v = prop_dictionary_get(obj, board_description_key);
575 1.2.2.2 christos if (v == NULL ||
576 1.2.2.2 christos prop_object_type(v) != PROP_TYPE_STRING)
577 1.2.2.2 christos return false;
578 1.2.2.2 christos
579 1.2.2.2 christos v = prop_dictionary_get(obj, board_u_boot_pkg_key);
580 1.2.2.2 christos if (v != NULL &&
581 1.2.2.2 christos (is_overlay || prop_object_type(v) != PROP_TYPE_STRING))
582 1.2.2.2 christos return false;
583 1.2.2.2 christos
584 1.2.2.2 christos /*
585 1.2.2.2 christos * "runtime-u-boot-path" is added to an overlay after we've
586 1.2.2.2 christos * validated the board object, so simply make sure it's not
587 1.2.2.2 christos * present.
588 1.2.2.2 christos */
589 1.2.2.2 christos v = prop_dictionary_get(obj, board_u_boot_path_key);
590 1.2.2.2 christos if (v != NULL)
591 1.2.2.2 christos return false;
592 1.2.2.2 christos
593 1.2.2.2 christos prop_object_iterator_t iter = prop_dictionary_iterator(obj);
594 1.2.2.2 christos prop_dictionary_keysym_t key;
595 1.2.2.2 christos while ((key = prop_object_iterator_next(iter)) != NULL) {
596 1.2.2.2 christos const char *cp = prop_dictionary_keysym_cstring_nocopy(key);
597 1.2.2.2 christos if (strcmp(cp, board_u_boot_install_key) == 0) {
598 1.2.2.2 christos has_default_install = true;
599 1.2.2.2 christos } else if (strncmp(cp, board_u_boot_install_key,
600 1.2.2.2 christos sizeof(board_u_boot_install_key) - 1) == 0 &&
601 1.2.2.2 christos cp[sizeof(board_u_boot_install_key) - 1] == '-') {
602 1.2.2.2 christos has_media_install = true;
603 1.2.2.2 christos } else {
604 1.2.2.2 christos continue;
605 1.2.2.2 christos }
606 1.2.2.2 christos v = prop_dictionary_get_keysym(obj, key);
607 1.2.2.2 christos assert(v != NULL);
608 1.2.2.2 christos if (!is_overlay || !validate_ubinstall_object(v))
609 1.2.2.2 christos break;
610 1.2.2.2 christos }
611 1.2.2.2 christos prop_object_iterator_release(iter);
612 1.2.2.2 christos if (key != NULL)
613 1.2.2.2 christos return false;
614 1.2.2.2 christos
615 1.2.2.2 christos /*
616 1.2.2.2 christos * Overlays must have only a default install key OR one or more
617 1.2.2.2 christos * media install keys.
618 1.2.2.2 christos */
619 1.2.2.2 christos if (is_overlay)
620 1.2.2.2 christos return has_default_install ^ has_media_install;
621 1.2.2.2 christos
622 1.2.2.2 christos /*
623 1.2.2.2 christos * Base board objects must have neither.
624 1.2.2.2 christos */
625 1.2.2.2 christos return (has_default_install | has_media_install) == false;
626 1.2.2.2 christos }
627 1.2.2.2 christos
628 1.2.2.2 christos /*
629 1.2.2.2 christos * evb_db_load_overlay --
630 1.2.2.2 christos * Load boards from an overlay file into the db.
631 1.2.2.2 christos */
632 1.2.2.2 christos static void
633 1.2.2.2 christos evb_db_load_overlay(ib_params *params, const char *path,
634 1.2.2.2 christos const char *runtime_uboot_path)
635 1.2.2.2 christos {
636 1.2.2.2 christos prop_dictionary_t overlay;
637 1.2.2.2 christos struct stat sb;
638 1.2.2.2 christos
639 1.2.2.2 christos if (params->flags & IB_VERBOSE)
640 1.2.2.2 christos printf("Loading '%s'.\n", path);
641 1.2.2.2 christos
642 1.2.2.2 christos if (stat(path, &sb) < 0) {
643 1.2.2.2 christos warn("'%s'", path);
644 1.2.2.2 christos return;
645 1.2.2.2 christos } else {
646 1.2.2.2 christos overlay = prop_dictionary_internalize_from_file(path);
647 1.2.2.2 christos if (overlay == NULL) {
648 1.2.2.2 christos warnx("unable to parse overlay '%s'", path);
649 1.2.2.2 christos return;
650 1.2.2.2 christos }
651 1.2.2.2 christos }
652 1.2.2.2 christos
653 1.2.2.2 christos /*
654 1.2.2.2 christos * Validate all of the board objects and add them to the board
655 1.2.2.2 christos * db, replacing any existing entries as we go.
656 1.2.2.2 christos */
657 1.2.2.2 christos prop_object_iterator_t iter = prop_dictionary_iterator(overlay);
658 1.2.2.2 christos prop_dictionary_keysym_t key;
659 1.2.2.2 christos prop_dictionary_t board;
660 1.2.2.2 christos while ((key = prop_object_iterator_next(iter)) != NULL) {
661 1.2.2.2 christos board = prop_dictionary_get_keysym(overlay, key);
662 1.2.2.2 christos assert(board != NULL);
663 1.2.2.2 christos if (!validate_board_object(board, true)) {
664 1.2.2.2 christos warnx("invalid board object in '%s': '%s'", path,
665 1.2.2.2 christos prop_dictionary_keysym_cstring_nocopy(key));
666 1.2.2.2 christos continue;
667 1.2.2.2 christos }
668 1.2.2.2 christos
669 1.2.2.2 christos /* Add "runtime-u-boot-path". */
670 1.2.2.2 christos prop_string_t string =
671 1.2.2.2 christos prop_string_create_cstring(runtime_uboot_path);
672 1.2.2.2 christos assert(string != NULL);
673 1.2.2.2 christos prop_dictionary_set(board, board_u_boot_path_key, string);
674 1.2.2.2 christos prop_object_release(string);
675 1.2.2.2 christos
676 1.2.2.2 christos /* Insert into board db. */
677 1.2.2.2 christos prop_dictionary_set_keysym(params->mach_data, key, board);
678 1.2.2.2 christos }
679 1.2.2.2 christos prop_object_iterator_release(iter);
680 1.2.2.2 christos prop_object_release(overlay);
681 1.2.2.2 christos }
682 1.2.2.2 christos
683 1.2.2.2 christos /*
684 1.2.2.2 christos * evb_db_load_overlays --
685 1.2.2.2 christos * Load the overlays from the search path.
686 1.2.2.2 christos */
687 1.2.2.2 christos static void
688 1.2.2.2 christos evb_db_load_overlays(ib_params *params)
689 1.2.2.2 christos {
690 1.2.2.2 christos char overlay_pathbuf[PATH_MAX+1];
691 1.2.2.2 christos const char *overlay_path;
692 1.2.2.2 christos char **paths;
693 1.2.2.2 christos void *pathsbuf = NULL;
694 1.2.2.2 christos FTS *fts;
695 1.2.2.2 christos FTSENT *chp, *p;
696 1.2.2.2 christos struct stat sb;
697 1.2.2.2 christos
698 1.2.2.2 christos paths = evb_uboot_pkg_paths(params, NULL, &pathsbuf);
699 1.2.2.2 christos if (paths == NULL) {
700 1.2.2.2 christos warnx("No u-boot search path?");
701 1.2.2.2 christos return;
702 1.2.2.2 christos }
703 1.2.2.2 christos
704 1.2.2.2 christos fts = fts_open(paths, FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOCHDIR, NULL);
705 1.2.2.2 christos if (fts == NULL ||
706 1.2.2.2 christos (chp = fts_children(fts, 0)) == NULL) {
707 1.2.2.2 christos warn("Unable to search u-boot path");
708 1.2.2.2 christos if (fts != NULL)
709 1.2.2.2 christos fts_close(fts);
710 1.2.2.2 christos return;
711 1.2.2.2 christos }
712 1.2.2.2 christos
713 1.2.2.2 christos chp = fts_children(fts, 0);
714 1.2.2.2 christos
715 1.2.2.2 christos while ((p = fts_read(fts)) != NULL) {
716 1.2.2.2 christos if (p->fts_info != FTS_D)
717 1.2.2.2 christos continue;
718 1.2.2.2 christos overlay_path = make_path(overlay_pathbuf,
719 1.2.2.2 christos sizeof(overlay_pathbuf), "%s/installboot.plist",
720 1.2.2.2 christos p->fts_path);
721 1.2.2.2 christos if (overlay_path == NULL)
722 1.2.2.2 christos continue;
723 1.2.2.2 christos if (stat(overlay_path, &sb) < 0)
724 1.2.2.2 christos continue;
725 1.2.2.2 christos evb_db_load_overlay(params, overlay_path, p->fts_path);
726 1.2.2.2 christos }
727 1.2.2.2 christos
728 1.2.2.2 christos fts_close(fts);
729 1.2.2.2 christos
730 1.2.2.2 christos /*
731 1.2.2.2 christos * If the user specifed a stage1 loader, then consult it last
732 1.2.2.2 christos * for a possible u-boot package location.
733 1.2.2.2 christos */
734 1.2.2.2 christos if (params->stage1 != NULL) {
735 1.2.2.2 christos overlay_path = make_path(overlay_pathbuf,
736 1.2.2.2 christos sizeof(overlay_pathbuf), "%s/installboot.plist",
737 1.2.2.2 christos params->stage1);
738 1.2.2.2 christos if (overlay_path != NULL) {
739 1.2.2.2 christos if (stat(overlay_path, &sb) == 0) {
740 1.2.2.2 christos evb_db_load_overlay(params, overlay_path,
741 1.2.2.2 christos params->stage1);
742 1.2.2.2 christos }
743 1.2.2.2 christos }
744 1.2.2.2 christos }
745 1.2.2.2 christos }
746 1.2.2.2 christos
747 1.2.2.2 christos /*
748 1.2.2.2 christos * evb_db_load_base --
749 1.2.2.2 christos * Load the base board db.
750 1.2.2.2 christos */
751 1.2.2.2 christos static bool
752 1.2.2.2 christos evb_db_load_base(ib_params *params)
753 1.2.2.2 christos {
754 1.2.2.2 christos char buf[PATH_MAX+1];
755 1.2.2.2 christos const char *path;
756 1.2.2.2 christos prop_dictionary_t board_db;
757 1.2.2.2 christos struct stat sb;
758 1.2.2.2 christos
759 1.2.2.2 christos path = evb_db_base_path(params, buf, sizeof(buf));
760 1.2.2.2 christos if (path == NULL)
761 1.2.2.2 christos return false;
762 1.2.2.2 christos
763 1.2.2.2 christos if (params->flags & IB_VERBOSE)
764 1.2.2.2 christos printf("Loading '%s'.\n", path);
765 1.2.2.2 christos
766 1.2.2.2 christos if (stat(path, &sb) < 0) {
767 1.2.2.2 christos if (errno != ENOENT) {
768 1.2.2.2 christos warn("'%s'", path);
769 1.2.2.2 christos return false;
770 1.2.2.2 christos }
771 1.2.2.2 christos board_db = prop_dictionary_create();
772 1.2.2.2 christos assert(board_db != NULL);
773 1.2.2.2 christos } else {
774 1.2.2.2 christos board_db = prop_dictionary_internalize_from_file(path);
775 1.2.2.2 christos if (board_db == NULL) {
776 1.2.2.2 christos warnx("unable to parse board db '%s'", path);
777 1.2.2.2 christos return false;
778 1.2.2.2 christos }
779 1.2.2.2 christos }
780 1.2.2.2 christos
781 1.2.2.2 christos if (prop_dictionary_count(board_db) == 0) {
782 1.2.2.2 christos /*
783 1.2.2.2 christos * Oh well, maybe we'll load some overlays.
784 1.2.2.2 christos */
785 1.2.2.2 christos goto done;
786 1.2.2.2 christos }
787 1.2.2.2 christos
788 1.2.2.2 christos /*
789 1.2.2.2 christos * Validate all of the board objects and remove any bad ones.
790 1.2.2.2 christos */
791 1.2.2.2 christos prop_array_t all_board_keys = prop_dictionary_all_keys(board_db);
792 1.2.2.2 christos prop_object_iterator_t iter = prop_array_iterator(all_board_keys);
793 1.2.2.2 christos prop_dictionary_keysym_t key;
794 1.2.2.2 christos prop_dictionary_t board;
795 1.2.2.2 christos while ((key = prop_object_iterator_next(iter)) != NULL) {
796 1.2.2.2 christos board = prop_dictionary_get_keysym(board_db, key);
797 1.2.2.2 christos assert(board != NULL);
798 1.2.2.2 christos if (!validate_board_object(board, false)) {
799 1.2.2.2 christos warnx("invalid board object in '%s': '%s'", path,
800 1.2.2.2 christos prop_dictionary_keysym_cstring_nocopy(key));
801 1.2.2.2 christos prop_dictionary_remove_keysym(board_db, key);
802 1.2.2.2 christos }
803 1.2.2.2 christos }
804 1.2.2.2 christos prop_object_iterator_release(iter);
805 1.2.2.2 christos prop_object_release(all_board_keys);
806 1.2.2.2 christos
807 1.2.2.2 christos done:
808 1.2.2.2 christos params->mach_data = board_db;
809 1.2.2.2 christos return true;
810 1.2.2.2 christos }
811 1.2.2.2 christos
812 1.2.2.2 christos /*
813 1.2.2.2 christos * evb_db_load --
814 1.2.2.2 christos * Load the board database.
815 1.2.2.2 christos */
816 1.2.2.2 christos bool
817 1.2.2.2 christos evb_db_load(ib_params *params)
818 1.2.2.2 christos {
819 1.2.2.2 christos if (!evb_db_load_base(params))
820 1.2.2.2 christos return false;
821 1.2.2.2 christos evb_db_load_overlays(params);
822 1.2.2.2 christos
823 1.2.2.2 christos return true;
824 1.2.2.2 christos }
825 1.2.2.2 christos
826 1.2.2.2 christos #if !HAVE_NBTOOL_CONFIG_H
827 1.2.2.2 christos /*
828 1.2.2.2 christos * Native board name guessing methods.
829 1.2.2.2 christos */
830 1.2.2.2 christos
831 1.2.2.2 christos #ifdef SUPPORT_OPENFIRMWARE
832 1.2.2.2 christos static int
833 1.2.2.2 christos ofw_fd(void)
834 1.2.2.2 christos {
835 1.2.2.2 christos static const char openfirm_path[] = "/dev/openfirm";
836 1.2.2.2 christos
837 1.2.2.2 christos return open(openfirm_path, O_RDONLY);
838 1.2.2.2 christos }
839 1.2.2.2 christos
840 1.2.2.2 christos static int
841 1.2.2.2 christos OF_finddevice(const char *name)
842 1.2.2.2 christos {
843 1.2.2.2 christos struct ofiocdesc ofio = {
844 1.2.2.2 christos .of_name = __UNCONST(name),
845 1.2.2.2 christos .of_namelen = strlen(name),
846 1.2.2.2 christos };
847 1.2.2.2 christos int fd = ofw_fd();
848 1.2.2.2 christos
849 1.2.2.2 christos if (fd == -1)
850 1.2.2.2 christos return -1;
851 1.2.2.2 christos
852 1.2.2.2 christos if (ioctl(fd, OFIOCFINDDEVICE, &ofio) < 0) {
853 1.2.2.2 christos if (errno != ENOENT)
854 1.2.2.2 christos warn("OFIOCFINDDEVICE('%s')", name);
855 1.2.2.2 christos ofio.of_nodeid = -1;
856 1.2.2.2 christos }
857 1.2.2.2 christos (void) close(fd);
858 1.2.2.2 christos
859 1.2.2.2 christos return ofio.of_nodeid;
860 1.2.2.2 christos }
861 1.2.2.2 christos
862 1.2.2.2 christos static int
863 1.2.2.2 christos OF_getprop(int phandle, const char *prop, void *buf, size_t buflen)
864 1.2.2.2 christos {
865 1.2.2.2 christos struct ofiocdesc ofio = {
866 1.2.2.2 christos .of_nodeid = phandle,
867 1.2.2.2 christos .of_name = __UNCONST(prop),
868 1.2.2.2 christos .of_namelen = strlen(prop),
869 1.2.2.2 christos .of_buf = buf,
870 1.2.2.2 christos .of_buflen = buflen,
871 1.2.2.2 christos };
872 1.2.2.2 christos int fd = ofw_fd();
873 1.2.2.2 christos
874 1.2.2.2 christos if (fd == -1)
875 1.2.2.2 christos return -1;
876 1.2.2.2 christos
877 1.2.2.2 christos int save_errno = 0;
878 1.2.2.2 christos
879 1.2.2.2 christos if (ioctl(fd, OFIOCGET, &ofio) < 0) {
880 1.2.2.2 christos save_errno = errno;
881 1.2.2.2 christos if (errno != ENOMEM && errno != ENOENT) {
882 1.2.2.2 christos save_errno = errno;
883 1.2.2.2 christos warn("OFIOCGET('%s')", prop);
884 1.2.2.2 christos }
885 1.2.2.2 christos ofio.of_buflen = -1;
886 1.2.2.2 christos }
887 1.2.2.2 christos (void) close(fd);
888 1.2.2.2 christos errno = save_errno;
889 1.2.2.2 christos
890 1.2.2.2 christos return ofio.of_buflen;
891 1.2.2.2 christos }
892 1.2.2.2 christos
893 1.2.2.2 christos static void *
894 1.2.2.2 christos ofw_getprop(int phandle, const char *prop, int *lenp)
895 1.2.2.2 christos {
896 1.2.2.2 christos size_t buflen = 32;
897 1.2.2.2 christos void *buf = NULL;
898 1.2.2.2 christos int len;
899 1.2.2.2 christos
900 1.2.2.2 christos for (;;) {
901 1.2.2.2 christos void *newbuf = realloc(buf, buflen);
902 1.2.2.2 christos if (newbuf == NULL) {
903 1.2.2.2 christos free(buf);
904 1.2.2.2 christos return NULL;
905 1.2.2.2 christos }
906 1.2.2.2 christos buf = newbuf;
907 1.2.2.2 christos switch (len = OF_getprop(phandle, prop, buf, buflen)) {
908 1.2.2.2 christos case -1:
909 1.2.2.2 christos if (errno != ENOMEM) {
910 1.2.2.2 christos free(buf);
911 1.2.2.2 christos return NULL;
912 1.2.2.2 christos }
913 1.2.2.2 christos buflen *= 2;
914 1.2.2.2 christos break;
915 1.2.2.2 christos
916 1.2.2.2 christos default:
917 1.2.2.2 christos if (lenp)
918 1.2.2.2 christos *lenp = len;
919 1.2.2.2 christos return buf;
920 1.2.2.2 christos }
921 1.2.2.2 christos }
922 1.2.2.2 christos }
923 1.2.2.2 christos
924 1.2.2.2 christos static evb_board
925 1.2.2.2 christos evb_db_get_board_from_ofw(ib_params *params, const char **board_namep)
926 1.2.2.2 christos {
927 1.2.2.2 christos int phandle;
928 1.2.2.2 christos int compatible_len = 0;
929 1.2.2.2 christos char *compatible_buf;
930 1.2.2.2 christos const char *sp, *nsp;
931 1.2.2.2 christos evb_board board;
932 1.2.2.2 christos
933 1.2.2.2 christos phandle = OF_finddevice("/");
934 1.2.2.2 christos if (phandle == -1) {
935 1.2.2.2 christos /* No OpenFirmware available. */
936 1.2.2.2 christos return NULL;
937 1.2.2.2 christos }
938 1.2.2.2 christos
939 1.2.2.2 christos compatible_buf = ofw_getprop(phandle, "compatible", &compatible_len);
940 1.2.2.2 christos
941 1.2.2.2 christos /*
942 1.2.2.2 christos * We just leak compatible_buf on success. Not a big deal since
943 1.2.2.2 christos * we are not a long-running process.
944 1.2.2.2 christos */
945 1.2.2.2 christos
946 1.2.2.2 christos sp = compatible_buf;
947 1.2.2.2 christos while (compatible_len &&
948 1.2.2.2 christos (nsp = memchr(sp, 0, compatible_len)) != NULL) {
949 1.2.2.2 christos if (params->flags & IB_VERBOSE)
950 1.2.2.2 christos printf("Checking OFW compatible string '%s'.\n", sp);
951 1.2.2.2 christos board = prop_dictionary_get(params->mach_data, sp);
952 1.2.2.2 christos if (board != NULL) {
953 1.2.2.2 christos if (board_namep)
954 1.2.2.2 christos *board_namep = sp;
955 1.2.2.2 christos return board;
956 1.2.2.2 christos }
957 1.2.2.2 christos nsp++; /* skip over NUL */
958 1.2.2.2 christos compatible_len -= (nsp - sp);
959 1.2.2.2 christos sp = nsp;
960 1.2.2.2 christos }
961 1.2.2.2 christos
962 1.2.2.2 christos free(compatible_buf);
963 1.2.2.2 christos return NULL;
964 1.2.2.2 christos }
965 1.2.2.2 christos #endif /* SUPPORT_OPENFIRMWARE */
966 1.2.2.2 christos
967 1.2.2.2 christos #endif /* ! HAVE_NBTOOL_CONFIG_H */
968 1.2.2.2 christos
969 1.2.2.2 christos /*
970 1.2.2.2 christos * Host-tool and native board name guessing methods.
971 1.2.2.2 christos */
972 1.2.2.2 christos
973 1.2.2.2 christos #ifdef SUPPORT_FDT
974 1.2.2.2 christos static void *
975 1.2.2.2 christos load_dtb(ib_params *params)
976 1.2.2.2 christos {
977 1.2.2.2 christos struct stat sb;
978 1.2.2.2 christos void *buf;
979 1.2.2.2 christos int fd;
980 1.2.2.2 christos
981 1.2.2.2 christos if (stat(params->dtb, &sb) < 0) {
982 1.2.2.2 christos warn("%s", params->dtb);
983 1.2.2.2 christos return NULL;
984 1.2.2.2 christos }
985 1.2.2.2 christos
986 1.2.2.2 christos buf = malloc((size_t)sb.st_size);
987 1.2.2.2 christos assert(buf != NULL);
988 1.2.2.2 christos
989 1.2.2.2 christos if ((fd = open(params->dtb, O_RDONLY)) < 0) {
990 1.2.2.2 christos warn("%s", params->dtb);
991 1.2.2.2 christos free(buf);
992 1.2.2.2 christos return NULL;
993 1.2.2.2 christos }
994 1.2.2.2 christos
995 1.2.2.2 christos if (read(fd, buf, (size_t)sb.st_size) != (ssize_t)sb.st_size) {
996 1.2.2.2 christos warn("read '%s'", params->dtb);
997 1.2.2.2 christos free(buf);
998 1.2.2.2 christos buf = NULL;
999 1.2.2.2 christos }
1000 1.2.2.2 christos (void) close(fd);
1001 1.2.2.2 christos
1002 1.2.2.2 christos return buf;
1003 1.2.2.2 christos }
1004 1.2.2.2 christos
1005 1.2.2.2 christos static evb_board
1006 1.2.2.2 christos evb_db_get_board_from_dtb(ib_params *params, const char **board_namep)
1007 1.2.2.2 christos {
1008 1.2.2.2 christos evb_board board = NULL;
1009 1.2.2.2 christos void *fdt = NULL;
1010 1.2.2.2 christos int error;
1011 1.2.2.2 christos
1012 1.2.2.2 christos fdt = load_dtb(params);
1013 1.2.2.2 christos if (fdt == NULL)
1014 1.2.2.2 christos return NULL;
1015 1.2.2.2 christos
1016 1.2.2.2 christos error = fdt_check_header(fdt);
1017 1.2.2.2 christos if (error) {
1018 1.2.2.2 christos warnx("%s: %s", params->dtb, fdt_strerror(error));
1019 1.2.2.2 christos goto bad;
1020 1.2.2.2 christos }
1021 1.2.2.2 christos
1022 1.2.2.2 christos const int system_root = fdt_path_offset(fdt, "/");
1023 1.2.2.2 christos if (system_root < 0) {
1024 1.2.2.2 christos warnx("%s: unable to find node '/'", params->dtb);
1025 1.2.2.2 christos goto bad;
1026 1.2.2.2 christos }
1027 1.2.2.2 christos
1028 1.2.2.2 christos const int system_ncompat = fdt_stringlist_count(fdt, system_root,
1029 1.2.2.2 christos "compatible");
1030 1.2.2.2 christos if (system_ncompat <= 0) {
1031 1.2.2.2 christos warnx("%s: no 'compatible' property on node '/'", params->dtb);
1032 1.2.2.2 christos goto bad;
1033 1.2.2.2 christos }
1034 1.2.2.2 christos
1035 1.2.2.2 christos const char *compatible;
1036 1.2.2.2 christos int si;
1037 1.2.2.2 christos for (si = 0; si < system_ncompat; si++) {
1038 1.2.2.2 christos compatible = fdt_stringlist_get(fdt, system_root,
1039 1.2.2.2 christos "compatible", si, NULL);
1040 1.2.2.2 christos if (compatible == NULL)
1041 1.2.2.2 christos continue;
1042 1.2.2.2 christos if (params->flags & IB_VERBOSE)
1043 1.2.2.2 christos printf("Checking FDT compatible string '%s'.\n",
1044 1.2.2.2 christos compatible);
1045 1.2.2.2 christos board = prop_dictionary_get(params->mach_data, compatible);
1046 1.2.2.2 christos if (board != NULL) {
1047 1.2.2.2 christos /*
1048 1.2.2.2 christos * We just leak compatible on success. Not a big
1049 1.2.2.2 christos * deal since we are not a long-running process.
1050 1.2.2.2 christos */
1051 1.2.2.2 christos if (board_namep) {
1052 1.2.2.2 christos *board_namep = strdup(compatible);
1053 1.2.2.2 christos assert(*board_namep != NULL);
1054 1.2.2.2 christos }
1055 1.2.2.2 christos free(fdt);
1056 1.2.2.2 christos return board;
1057 1.2.2.2 christos }
1058 1.2.2.2 christos }
1059 1.2.2.2 christos
1060 1.2.2.2 christos bad:
1061 1.2.2.2 christos if (fdt != NULL)
1062 1.2.2.2 christos free(fdt);
1063 1.2.2.2 christos return NULL;
1064 1.2.2.2 christos }
1065 1.2.2.2 christos #endif /* SUPPORT_FDT */
1066 1.2.2.2 christos
1067 1.2.2.2 christos /*
1068 1.2.2.2 christos * evb_db_get_board --
1069 1.2.2.2 christos * Return the specified board object from the database.
1070 1.2.2.2 christos */
1071 1.2.2.2 christos evb_board
1072 1.2.2.2 christos evb_db_get_board(ib_params *params)
1073 1.2.2.2 christos {
1074 1.2.2.2 christos const char *board_name = NULL;
1075 1.2.2.2 christos evb_board board = NULL;
1076 1.2.2.2 christos
1077 1.2.2.2 christos #if !HAVE_NBTOOL_CONFIG_H
1078 1.2.2.2 christos /*
1079 1.2.2.2 christos * If we're not a host tool, determine if we're running "natively".
1080 1.2.2.2 christos */
1081 1.2.2.2 christos bool is_native = false;
1082 1.2.2.2 christos struct utsname utsname;
1083 1.2.2.2 christos
1084 1.2.2.2 christos if (uname(&utsname) < 0) {
1085 1.2.2.2 christos warn("uname");
1086 1.2.2.2 christos } else if (strcmp(utsname.machine, params->machine->name) == 0) {
1087 1.2.2.2 christos is_native = true;
1088 1.2.2.2 christos }
1089 1.2.2.2 christos #endif /* ! HAVE_NBTOOL_CONFIG_H */
1090 1.2.2.2 christos
1091 1.2.2.2 christos /*
1092 1.2.2.2 christos * Logic for determing board type that can be shared by host-tool
1093 1.2.2.2 christos * and native builds goes here.
1094 1.2.2.2 christos */
1095 1.2.2.2 christos
1096 1.2.2.2 christos /*
1097 1.2.2.2 christos * Command-line argument trumps all.
1098 1.2.2.2 christos */
1099 1.2.2.2 christos if (params->flags & IB_BOARD) {
1100 1.2.2.2 christos board_name = params->board;
1101 1.2.2.2 christos }
1102 1.2.2.2 christos
1103 1.2.2.2 christos #ifdef SUPPORT_FDT
1104 1.2.2.2 christos if (board_name == NULL && (params->flags & IB_DTB)) {
1105 1.2.2.2 christos board = evb_db_get_board_from_dtb(params, &board_name);
1106 1.2.2.2 christos if ((params->flags & IB_VERBOSE) && board != NULL)
1107 1.2.2.2 christos printf("Found board '%s' from DTB data.\n", board_name);
1108 1.2.2.2 christos #if !HAVE_NBTOOL_CONFIG_H
1109 1.2.2.2 christos /*
1110 1.2.2.2 christos * If the user specified a DTB, then regardless of the
1111 1.2.2.2 christos * outcome, this is like specifying the board directly,
1112 1.2.2.2 christos * so native checks should be skipped.
1113 1.2.2.2 christos */
1114 1.2.2.2 christos is_native = false;
1115 1.2.2.2 christos #endif /* ! HAVE_NBTOOL_CONFIG_H */
1116 1.2.2.2 christos }
1117 1.2.2.2 christos #endif /* SUPPORT_FDT */
1118 1.2.2.2 christos
1119 1.2.2.2 christos #if !HAVE_NBTOOL_CONFIG_H
1120 1.2.2.2 christos /*
1121 1.2.2.2 christos * Non-host-tool logic for determining the board type goes here.
1122 1.2.2.2 christos */
1123 1.2.2.2 christos
1124 1.2.2.2 christos #ifdef SUPPORT_OPENFIRMWARE
1125 1.2.2.2 christos if (board_name == NULL && is_native) {
1126 1.2.2.2 christos board = evb_db_get_board_from_ofw(params, &board_name);
1127 1.2.2.2 christos if ((params->flags & IB_VERBOSE) && board != NULL)
1128 1.2.2.2 christos printf("Found board '%s' from OFW data.\n", board_name);
1129 1.2.2.2 christos }
1130 1.2.2.2 christos #endif /* SUPPORT_OPENFIRMWARE */
1131 1.2.2.2 christos
1132 1.2.2.2 christos /* Ensure is_native is consumed. */
1133 1.2.2.2 christos if (is_native == false)
1134 1.2.2.2 christos is_native = false;
1135 1.2.2.2 christos
1136 1.2.2.2 christos #endif /* ! HAVE_NBTOOL_CONFIG_H */
1137 1.2.2.2 christos
1138 1.2.2.2 christos /*
1139 1.2.2.2 christos * If all else fails, we can always rely on the user, right?
1140 1.2.2.2 christos */
1141 1.2.2.2 christos if (board_name == NULL) {
1142 1.2.2.2 christos if (!(params->flags & IB_BOARD)) {
1143 1.2.2.2 christos warnx("Must specify board=...");
1144 1.2.2.2 christos return NULL;
1145 1.2.2.2 christos }
1146 1.2.2.2 christos board_name = params->board;
1147 1.2.2.2 christos }
1148 1.2.2.2 christos
1149 1.2.2.2 christos assert(board_name != NULL);
1150 1.2.2.2 christos
1151 1.2.2.2 christos if (board == NULL)
1152 1.2.2.2 christos board = prop_dictionary_get(params->mach_data, board_name);
1153 1.2.2.2 christos if (board == NULL)
1154 1.2.2.2 christos warnx("Unknown board '%s'", board_name);
1155 1.2.2.2 christos
1156 1.2.2.2 christos /* Ensure params->board is always valid. */
1157 1.2.2.2 christos params->board = board_name;
1158 1.2.2.2 christos
1159 1.2.2.2 christos if (params->flags & IB_VERBOSE) {
1160 1.2.2.2 christos printf("Board: %s\n", evb_board_get_description(params, board));
1161 1.2.2.2 christos }
1162 1.2.2.2 christos
1163 1.2.2.2 christos return board;
1164 1.2.2.2 christos }
1165 1.2.2.2 christos
1166 1.2.2.2 christos /*
1167 1.2.2.2 christos * evb_db_list_boards --
1168 1.2.2.2 christos * Print the list of known boards to the specified output stream.
1169 1.2.2.2 christos */
1170 1.2.2.2 christos void
1171 1.2.2.2 christos evb_db_list_boards(ib_params *params, FILE *out)
1172 1.2.2.2 christos {
1173 1.2.2.2 christos prop_object_iterator_t iter;
1174 1.2.2.2 christos prop_dictionary_keysym_t key;
1175 1.2.2.2 christos evb_board board;
1176 1.2.2.2 christos const char *uboot_pkg;
1177 1.2.2.2 christos const char *uboot_path;
1178 1.2.2.2 christos
1179 1.2.2.2 christos /*
1180 1.2.2.2 christos * By default, we only list boards that we have a u-boot
1181 1.2.2.2 christos * package installed for, or if we know which package you
1182 1.2.2.2 christos * need to install. You get the full monty in verbose mode.
1183 1.2.2.2 christos */
1184 1.2.2.2 christos
1185 1.2.2.2 christos iter = prop_dictionary_iterator(params->mach_data);
1186 1.2.2.2 christos while ((key = prop_object_iterator_next(iter)) != NULL) {
1187 1.2.2.2 christos board = prop_dictionary_get_keysym(params->mach_data, key);
1188 1.2.2.2 christos assert(board != NULL);
1189 1.2.2.2 christos uboot_pkg = evb_board_get_uboot_pkg(params, board);
1190 1.2.2.2 christos uboot_path = evb_board_get_uboot_path(params, board);
1191 1.2.2.2 christos
1192 1.2.2.2 christos if (uboot_pkg == NULL && uboot_path == NULL &&
1193 1.2.2.2 christos !(params->flags & IB_VERBOSE))
1194 1.2.2.2 christos continue;
1195 1.2.2.2 christos
1196 1.2.2.2 christos fprintf(out, "%-30s %s\n",
1197 1.2.2.2 christos prop_dictionary_keysym_cstring_nocopy(key),
1198 1.2.2.2 christos evb_board_get_description(params, board));
1199 1.2.2.2 christos
1200 1.2.2.2 christos if ((params->flags & IB_VERBOSE) && uboot_path) {
1201 1.2.2.2 christos fprintf(out, "\t(u-boot package found at %s)\n",
1202 1.2.2.2 christos uboot_path);
1203 1.2.2.2 christos } else if ((params->flags & IB_VERBOSE) && uboot_pkg) {
1204 1.2.2.2 christos fprintf(out,
1205 1.2.2.2 christos "\t(install the sysutils/u-boot-%s package)\n",
1206 1.2.2.2 christos uboot_pkg);
1207 1.2.2.2 christos }
1208 1.2.2.2 christos }
1209 1.2.2.2 christos prop_object_iterator_release(iter);
1210 1.2.2.2 christos }
1211 1.2.2.2 christos
1212 1.2.2.2 christos /*
1213 1.2.2.2 christos * evb_board_get_description --
1214 1.2.2.2 christos * Return the description for the specified board.
1215 1.2.2.2 christos */
1216 1.2.2.2 christos const char *
1217 1.2.2.2 christos evb_board_get_description(ib_params *params, evb_board board)
1218 1.2.2.2 christos {
1219 1.2.2.2 christos prop_string_t string;
1220 1.2.2.2 christos
1221 1.2.2.2 christos string = prop_dictionary_get(board, board_description_key);
1222 1.2.2.2 christos return prop_string_cstring_nocopy(string);
1223 1.2.2.2 christos }
1224 1.2.2.2 christos
1225 1.2.2.2 christos /*
1226 1.2.2.2 christos * evb_board_get_uboot_pkg --
1227 1.2.2.2 christos * Return the u-boot package name for the specified board.
1228 1.2.2.2 christos */
1229 1.2.2.2 christos const char *
1230 1.2.2.2 christos evb_board_get_uboot_pkg(ib_params *params, evb_board board)
1231 1.2.2.2 christos {
1232 1.2.2.2 christos prop_string_t string;
1233 1.2.2.2 christos
1234 1.2.2.2 christos string = prop_dictionary_get(board, board_u_boot_pkg_key);
1235 1.2.2.2 christos if (string == NULL)
1236 1.2.2.2 christos return NULL;
1237 1.2.2.2 christos return prop_string_cstring_nocopy(string);
1238 1.2.2.2 christos }
1239 1.2.2.2 christos
1240 1.2.2.2 christos /*
1241 1.2.2.2 christos * evb_board_get_uboot_path --
1242 1.2.2.2 christos * Return the u-boot installed package path for the specified board.
1243 1.2.2.2 christos */
1244 1.2.2.2 christos const char *
1245 1.2.2.2 christos evb_board_get_uboot_path(ib_params *params, evb_board board)
1246 1.2.2.2 christos {
1247 1.2.2.2 christos prop_string_t string;
1248 1.2.2.2 christos
1249 1.2.2.2 christos string = prop_dictionary_get(board, board_u_boot_path_key);
1250 1.2.2.2 christos if (string == NULL)
1251 1.2.2.2 christos return NULL;
1252 1.2.2.2 christos return prop_string_cstring_nocopy(string);
1253 1.2.2.2 christos }
1254 1.2.2.2 christos
1255 1.2.2.2 christos /*
1256 1.2.2.2 christos * evb_board_get_uboot_install --
1257 1.2.2.2 christos * Return the u-boot install object for the specified board,
1258 1.2.2.2 christos * corresponding to the media specified by the user.
1259 1.2.2.2 christos */
1260 1.2.2.2 christos evb_ubinstall
1261 1.2.2.2 christos evb_board_get_uboot_install(ib_params *params, evb_board board)
1262 1.2.2.2 christos {
1263 1.2.2.2 christos evb_ubinstall install;
1264 1.2.2.2 christos
1265 1.2.2.2 christos install = prop_dictionary_get(board, board_u_boot_install_key);
1266 1.2.2.2 christos
1267 1.2.2.2 christos if (!(params->flags & IB_MEDIA)) {
1268 1.2.2.2 christos if (install == NULL) {
1269 1.2.2.2 christos warnx("Must specify media=... for board '%s'",
1270 1.2.2.2 christos params->board);
1271 1.2.2.2 christos goto list_media;
1272 1.2.2.2 christos }
1273 1.2.2.2 christos return install;
1274 1.2.2.2 christos }
1275 1.2.2.2 christos
1276 1.2.2.2 christos /* media=... was specified by the user. */
1277 1.2.2.2 christos
1278 1.2.2.2 christos if (install) {
1279 1.2.2.2 christos warnx("media=... is not a valid option for board '%s'",
1280 1.2.2.2 christos params->board);
1281 1.2.2.2 christos return NULL;
1282 1.2.2.2 christos }
1283 1.2.2.2 christos
1284 1.2.2.2 christos char install_key[128];
1285 1.2.2.2 christos int n = snprintf(install_key, sizeof(install_key), "%s-%s",
1286 1.2.2.2 christos board_u_boot_install_key, params->media);
1287 1.2.2.2 christos if (n < 0 || (size_t)n >= sizeof(install_key))
1288 1.2.2.2 christos goto invalid_media;;
1289 1.2.2.2 christos install = prop_dictionary_get(board, install_key);
1290 1.2.2.2 christos if (install != NULL)
1291 1.2.2.2 christos return install;
1292 1.2.2.2 christos invalid_media:
1293 1.2.2.2 christos warnx("invalid media specification: '%s'", params->media);
1294 1.2.2.2 christos list_media:
1295 1.2.2.2 christos fprintf(stderr, "Valid media types:");
1296 1.2.2.2 christos prop_array_t array = evb_board_copy_uboot_media(params, board);
1297 1.2.2.2 christos assert(array != NULL);
1298 1.2.2.2 christos prop_object_iterator_t iter = prop_array_iterator(array);
1299 1.2.2.2 christos prop_string_t string;
1300 1.2.2.2 christos while ((string = prop_object_iterator_next(iter)) != NULL)
1301 1.2.2.2 christos fprintf(stderr, " %s", prop_string_cstring_nocopy(string));
1302 1.2.2.2 christos fprintf(stderr, "\n");
1303 1.2.2.2 christos prop_object_iterator_release(iter);
1304 1.2.2.2 christos prop_object_release(array);
1305 1.2.2.2 christos
1306 1.2.2.2 christos return NULL;
1307 1.2.2.2 christos }
1308 1.2.2.2 christos
1309 1.2.2.2 christos /*
1310 1.2.2.2 christos * evb_board_copy_uboot_media --
1311 1.2.2.2 christos * Return the valid media types for the given board as an array
1312 1.2.2.2 christos * of strings.
1313 1.2.2.2 christos *
1314 1.2.2.2 christos * Follows the create rule; caller is responsible for releasing
1315 1.2.2.2 christos * the array.
1316 1.2.2.2 christos */
1317 1.2.2.2 christos prop_array_t
1318 1.2.2.2 christos evb_board_copy_uboot_media(ib_params *params, evb_board board)
1319 1.2.2.2 christos {
1320 1.2.2.2 christos prop_array_t array = prop_array_create();
1321 1.2.2.2 christos prop_object_iterator_t iter = prop_dictionary_iterator(board);
1322 1.2.2.2 christos prop_string_t string;
1323 1.2.2.2 christos prop_dictionary_keysym_t key;
1324 1.2.2.2 christos const char *cp;
1325 1.2.2.2 christos
1326 1.2.2.2 christos assert(array != NULL);
1327 1.2.2.2 christos assert(iter != NULL);
1328 1.2.2.2 christos
1329 1.2.2.2 christos while ((key = prop_object_iterator_next(iter)) != NULL) {
1330 1.2.2.2 christos cp = prop_dictionary_keysym_cstring_nocopy(key);
1331 1.2.2.2 christos if (strcmp(cp, board_u_boot_install_key) == 0 ||
1332 1.2.2.2 christos strncmp(cp, board_u_boot_install_key,
1333 1.2.2.2 christos sizeof(board_u_boot_install_key) - 1) != 0)
1334 1.2.2.2 christos continue;
1335 1.2.2.2 christos string = prop_string_create_cstring(strrchr(cp, '-')+1);
1336 1.2.2.2 christos assert(string != NULL);
1337 1.2.2.2 christos prop_array_add(array, string);
1338 1.2.2.2 christos prop_object_release(string);
1339 1.2.2.2 christos }
1340 1.2.2.2 christos prop_object_iterator_release(iter);
1341 1.2.2.2 christos return array;
1342 1.2.2.2 christos }
1343 1.2.2.2 christos
1344 1.2.2.2 christos /*
1345 1.2.2.2 christos * evb_ubinstall_get_steps --
1346 1.2.2.2 christos * Get the install steps for a given install object.
1347 1.2.2.2 christos */
1348 1.2.2.2 christos evb_ubsteps
1349 1.2.2.2 christos evb_ubinstall_get_steps(ib_params *params, evb_ubinstall install)
1350 1.2.2.2 christos {
1351 1.2.2.2 christos return prop_array_iterator(install);
1352 1.2.2.2 christos }
1353 1.2.2.2 christos
1354 1.2.2.2 christos /*
1355 1.2.2.2 christos * evb_ubsteps_next_step --
1356 1.2.2.2 christos * Return the next step in the install object.
1357 1.2.2.2 christos *
1358 1.2.2.2 christos * N.B. The iterator is released upon termination.
1359 1.2.2.2 christos */
1360 1.2.2.2 christos evb_ubstep
1361 1.2.2.2 christos evb_ubsteps_next_step(ib_params *params, evb_ubsteps steps)
1362 1.2.2.2 christos {
1363 1.2.2.2 christos prop_dictionary_t step = prop_object_iterator_next(steps);
1364 1.2.2.2 christos
1365 1.2.2.2 christos /* If we are out of steps, release the iterator. */
1366 1.2.2.2 christos if (step == NULL)
1367 1.2.2.2 christos prop_object_iterator_release(steps);
1368 1.2.2.2 christos
1369 1.2.2.2 christos return step;
1370 1.2.2.2 christos }
1371 1.2.2.2 christos
1372 1.2.2.2 christos /*
1373 1.2.2.2 christos * evb_ubstep_get_file_name --
1374 1.2.2.2 christos * Returns the input file name for the step.
1375 1.2.2.2 christos */
1376 1.2.2.2 christos const char *
1377 1.2.2.2 christos evb_ubstep_get_file_name(ib_params *params, evb_ubstep step)
1378 1.2.2.2 christos {
1379 1.2.2.2 christos prop_string_t string = prop_dictionary_get(step, step_file_name_key);
1380 1.2.2.2 christos return prop_string_cstring_nocopy(string);
1381 1.2.2.2 christos }
1382 1.2.2.2 christos
1383 1.2.2.2 christos /*
1384 1.2.2.2 christos * evb_ubstep_get_file_offset --
1385 1.2.2.2 christos * Returns the input file offset for the step.
1386 1.2.2.2 christos */
1387 1.2.2.2 christos uint64_t
1388 1.2.2.2 christos evb_ubstep_get_file_offset(ib_params *params, evb_ubstep step)
1389 1.2.2.2 christos {
1390 1.2.2.2 christos prop_number_t number = prop_dictionary_get(step, step_file_offset_key);
1391 1.2.2.2 christos if (number != NULL)
1392 1.2.2.2 christos return prop_number_unsigned_integer_value(number);
1393 1.2.2.2 christos return 0;
1394 1.2.2.2 christos }
1395 1.2.2.2 christos
1396 1.2.2.2 christos /*
1397 1.2.2.2 christos * evb_ubstep_get_file_size --
1398 1.2.2.2 christos * Returns the size of the input file to copy for this step, or
1399 1.2.2.2 christos * zero if the remainder of the file should be copied.
1400 1.2.2.2 christos */
1401 1.2.2.2 christos uint64_t
1402 1.2.2.2 christos evb_ubstep_get_file_size(ib_params *params, evb_ubstep step)
1403 1.2.2.2 christos {
1404 1.2.2.2 christos prop_number_t number = prop_dictionary_get(step, step_file_size_key);
1405 1.2.2.2 christos if (number != NULL)
1406 1.2.2.2 christos return prop_number_unsigned_integer_value(number);
1407 1.2.2.2 christos return 0;
1408 1.2.2.2 christos }
1409 1.2.2.2 christos
1410 1.2.2.2 christos /*
1411 1.2.2.2 christos * evb_ubstep_get_image_offset --
1412 1.2.2.2 christos * Returns the offset into the destination image / device to
1413 1.2.2.2 christos * copy the input file.
1414 1.2.2.2 christos */
1415 1.2.2.2 christos uint64_t
1416 1.2.2.2 christos evb_ubstep_get_image_offset(ib_params *params, evb_ubstep step)
1417 1.2.2.2 christos {
1418 1.2.2.2 christos prop_number_t number = prop_dictionary_get(step, step_image_offset_key);
1419 1.2.2.2 christos if (number != NULL)
1420 1.2.2.2 christos return prop_number_unsigned_integer_value(number);
1421 1.2.2.2 christos return 0;
1422 1.2.2.2 christos }
1423 1.2.2.2 christos
1424 1.2.2.2 christos /*
1425 1.2.2.2 christos * evb_ubstep_preserves_partial_block --
1426 1.2.2.2 christos * Returns true if the step preserves a partial block.
1427 1.2.2.2 christos */
1428 1.2.2.2 christos bool
1429 1.2.2.2 christos evb_ubstep_preserves_partial_block(ib_params *params, evb_ubstep step)
1430 1.2.2.2 christos {
1431 1.2.2.2 christos prop_bool_t val = prop_dictionary_get(step, step_preserve_key);
1432 1.2.2.2 christos if (val != NULL)
1433 1.2.2.2 christos return prop_bool_true(val);
1434 1.2.2.2 christos return false;
1435 1.2.2.2 christos }
1436 1.2.2.2 christos
1437 1.2.2.2 christos /*
1438 1.2.2.2 christos * evb_uboot_file_path --
1439 1.2.2.2 christos * Build a file path from the u-boot base path in the board object
1440 1.2.2.2 christos * and the file name in the step object.
1441 1.2.2.2 christos */
1442 1.2.2.2 christos static const char *
1443 1.2.2.2 christos evb_uboot_file_path(ib_params *params, evb_board board, evb_ubstep step,
1444 1.2.2.2 christos char *buf, size_t bufsize)
1445 1.2.2.2 christos {
1446 1.2.2.2 christos const char *base_path = evb_board_get_uboot_path(params, board);
1447 1.2.2.2 christos const char *file_name = evb_ubstep_get_file_name(params, step);
1448 1.2.2.2 christos
1449 1.2.2.2 christos if (base_path == NULL || file_name == NULL)
1450 1.2.2.2 christos return NULL;
1451 1.2.2.2 christos
1452 1.2.2.2 christos return make_path(buf, bufsize, "%s/%s", base_path, file_name);
1453 1.2.2.2 christos }
1454 1.2.2.2 christos
1455 1.2.2.2 christos /*
1456 1.2.2.2 christos * evb_uboot_do_step --
1457 1.2.2.2 christos * Given a evb_ubstep, do the deed.
1458 1.2.2.2 christos */
1459 1.2.2.2 christos static int
1460 1.2.2.2 christos evb_uboot_do_step(ib_params *params, const char *uboot_file, evb_ubstep step)
1461 1.2.2.2 christos {
1462 1.2.2.2 christos struct stat sb;
1463 1.2.2.2 christos int ifd = -1;
1464 1.2.2.2 christos char *blockbuf;
1465 1.2.2.2 christos size_t thisblock;
1466 1.2.2.2 christos off_t curoffset;
1467 1.2.2.2 christos off_t remaining;
1468 1.2.2.2 christos bool rv = false;
1469 1.2.2.2 christos
1470 1.2.2.2 christos uint64_t file_size = evb_ubstep_get_file_size(params, step);
1471 1.2.2.2 christos uint64_t file_offset = evb_ubstep_get_file_offset(params, step);
1472 1.2.2.2 christos uint64_t image_offset = evb_ubstep_get_image_offset(params, step);
1473 1.2.2.2 christos
1474 1.2.2.2 christos blockbuf = malloc(params->sectorsize);
1475 1.2.2.2 christos if (blockbuf == NULL)
1476 1.2.2.2 christos goto out;
1477 1.2.2.2 christos
1478 1.2.2.2 christos ifd = open(uboot_file, O_RDONLY);
1479 1.2.2.2 christos if (ifd < 0) {
1480 1.2.2.2 christos warn("open '%s'", uboot_file);
1481 1.2.2.2 christos goto out;
1482 1.2.2.2 christos }
1483 1.2.2.2 christos if (fstat(ifd, &sb) < 0) {
1484 1.2.2.2 christos warn("fstat '%s'", uboot_file);
1485 1.2.2.2 christos goto out;
1486 1.2.2.2 christos }
1487 1.2.2.2 christos
1488 1.2.2.2 christos if (file_size)
1489 1.2.2.2 christos remaining = (off_t)file_size;
1490 1.2.2.2 christos else
1491 1.2.2.2 christos remaining = sb.st_size - (off_t)file_offset;
1492 1.2.2.2 christos
1493 1.2.2.2 christos if (params->flags & IB_VERBOSE) {
1494 1.2.2.2 christos if (file_offset) {
1495 1.2.2.2 christos printf("Writing '%s' -- %lld @ %" PRIu64 " ==> %" PRIu64 "\n",
1496 1.2.2.2 christos evb_ubstep_get_file_name(params, step),
1497 1.2.2.2 christos (long long)remaining, file_offset, image_offset);
1498 1.2.2.2 christos } else {
1499 1.2.2.2 christos printf("Writing '%s' -- %lld ==> %" PRIu64 "\n",
1500 1.2.2.2 christos evb_ubstep_get_file_name(params, step),
1501 1.2.2.2 christos (long long)remaining, image_offset);
1502 1.2.2.2 christos }
1503 1.2.2.2 christos }
1504 1.2.2.2 christos
1505 1.2.2.2 christos if (lseek(ifd, (off_t)file_offset, SEEK_SET) < 0) {
1506 1.2.2.2 christos warn("lseek '%s' @ %" PRIu64, uboot_file,
1507 1.2.2.2 christos file_offset);
1508 1.2.2.2 christos goto out;
1509 1.2.2.2 christos }
1510 1.2.2.2 christos
1511 1.2.2.2 christos for (curoffset = (off_t)image_offset; remaining > 0;
1512 1.2.2.2 christos remaining -= thisblock, curoffset += params->sectorsize) {
1513 1.2.2.2 christos thisblock = params->sectorsize;
1514 1.2.2.2 christos if ((off_t)thisblock > remaining)
1515 1.2.2.2 christos thisblock = (size_t)remaining;
1516 1.2.2.2 christos if ((thisblock % params->sectorsize) != 0) {
1517 1.2.2.2 christos memset(blockbuf, 0, params->sectorsize);
1518 1.2.2.2 christos if (evb_ubstep_preserves_partial_block(params, step)) {
1519 1.2.2.2 christos if (params->flags & IB_VERBOSE) {
1520 1.2.2.2 christos printf("(Reading '%s' -- %u @ %lld)\n",
1521 1.2.2.2 christos params->filesystem,
1522 1.2.2.2 christos params->sectorsize,
1523 1.2.2.2 christos (long long)curoffset);
1524 1.2.2.2 christos }
1525 1.2.2.2 christos if (pread(params->fsfd, blockbuf,
1526 1.2.2.2 christos params->sectorsize, curoffset) < 0) {
1527 1.2.2.2 christos warn("pread '%s'", params->filesystem);
1528 1.2.2.2 christos goto out;
1529 1.2.2.2 christos }
1530 1.2.2.2 christos }
1531 1.2.2.2 christos }
1532 1.2.2.2 christos if (read(ifd, blockbuf, thisblock) != (ssize_t)thisblock) {
1533 1.2.2.2 christos warn("read '%s'", uboot_file);
1534 1.2.2.2 christos goto out;
1535 1.2.2.2 christos }
1536 1.2.2.2 christos if (!(params->flags & IB_NOWRITE) &&
1537 1.2.2.2 christos pwrite(params->fsfd, blockbuf, params->sectorsize,
1538 1.2.2.2 christos curoffset) != (ssize_t)params->sectorsize) {
1539 1.2.2.2 christos warn("pwrite '%s'", params->filesystem);
1540 1.2.2.2 christos goto out;
1541 1.2.2.2 christos }
1542 1.2.2.2 christos }
1543 1.2.2.2 christos
1544 1.2.2.2 christos /* Success! */
1545 1.2.2.2 christos rv = true;
1546 1.2.2.2 christos
1547 1.2.2.2 christos out:
1548 1.2.2.2 christos if (ifd != -1 && close(ifd) == -1)
1549 1.2.2.2 christos warn("close '%s'", uboot_file);
1550 1.2.2.2 christos if (blockbuf)
1551 1.2.2.2 christos free(blockbuf);
1552 1.2.2.2 christos return rv;
1553 1.2.2.2 christos }
1554 1.2.2.2 christos
1555 1.2.2.2 christos int
1556 1.2.2.2 christos evb_uboot_setboot(ib_params *params, evb_board board)
1557 1.2.2.2 christos {
1558 1.2.2.2 christos char uboot_filebuf[PATH_MAX+1];
1559 1.2.2.2 christos const char *uboot_file;
1560 1.2.2.2 christos struct stat sb;
1561 1.2.2.2 christos off_t max_offset = 0;
1562 1.2.2.2 christos
1563 1.2.2.2 christos /*
1564 1.2.2.2 christos * If we don't have a u-boot path for this board, it means
1565 1.2.2.2 christos * that a u-boot package wasn't found. Prompt the user to
1566 1.2.2.2 christos * install it.
1567 1.2.2.2 christos */
1568 1.2.2.2 christos if (evb_board_get_uboot_path(params, board) == NULL) {
1569 1.2.2.2 christos warnx("No u-boot package found for board '%s'",
1570 1.2.2.2 christos params->board);
1571 1.2.2.2 christos uboot_file = evb_board_get_uboot_pkg(params, board);
1572 1.2.2.2 christos if (uboot_file != NULL)
1573 1.2.2.2 christos warnx("Please install the sysutils/u-boot-%s package.",
1574 1.2.2.2 christos uboot_file);
1575 1.2.2.2 christos return 0;
1576 1.2.2.2 christos }
1577 1.2.2.2 christos
1578 1.2.2.2 christos evb_ubinstall install = evb_board_get_uboot_install(params, board);
1579 1.2.2.2 christos evb_ubsteps steps;
1580 1.2.2.2 christos evb_ubstep step;
1581 1.2.2.2 christos
1582 1.2.2.2 christos if (install == NULL)
1583 1.2.2.2 christos return 0;
1584 1.2.2.2 christos
1585 1.2.2.2 christos /*
1586 1.2.2.2 christos * First, make sure the files are all there. While we're
1587 1.2.2.2 christos * at it, calculate the largest byte offset that we will
1588 1.2.2.2 christos * be writing.
1589 1.2.2.2 christos */
1590 1.2.2.2 christos steps = evb_ubinstall_get_steps(params, install);
1591 1.2.2.2 christos while ((step = evb_ubsteps_next_step(params, steps)) != NULL) {
1592 1.2.2.2 christos uint64_t file_offset = evb_ubstep_get_file_offset(params, step);
1593 1.2.2.2 christos uint64_t file_size = evb_ubstep_get_file_size(params, step);
1594 1.2.2.2 christos uint64_t image_offset =
1595 1.2.2.2 christos evb_ubstep_get_image_offset(params, step);
1596 1.2.2.2 christos uboot_file = evb_uboot_file_path(params, board, step,
1597 1.2.2.2 christos uboot_filebuf, sizeof(uboot_filebuf));
1598 1.2.2.2 christos if (uboot_file == NULL)
1599 1.2.2.2 christos return 0;
1600 1.2.2.2 christos if (stat(uboot_file, &sb) < 0) {
1601 1.2.2.2 christos warn("%s", uboot_file);
1602 1.2.2.2 christos return 0;
1603 1.2.2.2 christos }
1604 1.2.2.2 christos if (!S_ISREG(sb.st_mode)) {
1605 1.2.2.2 christos warnx("%s: %s", uboot_file, strerror(EFTYPE));
1606 1.2.2.2 christos return 0;
1607 1.2.2.2 christos }
1608 1.2.2.2 christos off_t this_max;
1609 1.2.2.2 christos if (file_size)
1610 1.2.2.2 christos this_max = file_size;
1611 1.2.2.2 christos else
1612 1.2.2.2 christos this_max = sb.st_size - file_offset;
1613 1.2.2.2 christos this_max += image_offset;
1614 1.2.2.2 christos if (max_offset < this_max)
1615 1.2.2.2 christos max_offset = this_max;
1616 1.2.2.2 christos }
1617 1.2.2.2 christos
1618 1.2.2.2 christos /*
1619 1.2.2.2 christos * Ok, we've verified that all of the files are there, and now
1620 1.2.2.2 christos * max_offset points to the first byte that's available for a
1621 1.2.2.2 christos * partition containing a file system.
1622 1.2.2.2 christos */
1623 1.2.2.2 christos
1624 1.2.2.2 christos off_t rounded_max_offset = (off_t)(max_offset / params->sectorsize) *
1625 1.2.2.2 christos params->sectorsize;
1626 1.2.2.2 christos if (rounded_max_offset != max_offset)
1627 1.2.2.2 christos rounded_max_offset += params->sectorsize;
1628 1.2.2.2 christos
1629 1.2.2.2 christos if (params->flags & IB_VERBOSE) {
1630 1.2.2.2 christos printf("Max u-boot offset (rounded): %lld (%lld)\n",
1631 1.2.2.2 christos (long long)max_offset, (long long)rounded_max_offset);
1632 1.2.2.2 christos printf("First free block available for file systems: "
1633 1.2.2.2 christos "%lld (0x%llx)\n",
1634 1.2.2.2 christos (long long)rounded_max_offset / params->sectorsize,
1635 1.2.2.2 christos (long long)rounded_max_offset / params->sectorsize);
1636 1.2.2.2 christos }
1637 1.2.2.2 christos
1638 1.2.2.2 christos /* XXX Check MBR table for overlapping partitions. */
1639 1.2.2.2 christos
1640 1.2.2.2 christos /*
1641 1.2.2.2 christos * Now write each binary component to the appropriate location
1642 1.2.2.2 christos * on disk.
1643 1.2.2.2 christos */
1644 1.2.2.2 christos steps = evb_ubinstall_get_steps(params, install);
1645 1.2.2.2 christos while ((step = evb_ubsteps_next_step(params, steps)) != NULL) {
1646 1.2.2.2 christos uboot_file = evb_uboot_file_path(params, board, step,
1647 1.2.2.2 christos uboot_filebuf, sizeof(uboot_filebuf));
1648 1.2.2.2 christos if (uboot_file == NULL)
1649 1.2.2.2 christos return 0;
1650 1.2.2.2 christos if (!evb_uboot_do_step(params, uboot_file, step))
1651 1.2.2.2 christos return 0;
1652 1.2.2.2 christos }
1653 1.2.2.2 christos
1654 1.2.2.2 christos return 1;
1655 1.2.2.2 christos }
1656