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