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