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