xref: /netbsd-src/usr.sbin/installboot/evboards.c (revision a4d52a8bafa0c5b0f585ec80a7499256c3677a93)
1*a4d52a8bSandvar /*	$NetBSD: evboards.c,v 1.8 2022/11/01 19:51:46 andvar Exp $	*/
202d67d10Sthorpej 
302d67d10Sthorpej /*-
402d67d10Sthorpej  * Copyright (c) 2019 The NetBSD Foundation, Inc.
502d67d10Sthorpej  * All rights reserved.
602d67d10Sthorpej  *
702d67d10Sthorpej  * This code is derived from software contributed to The NetBSD Foundation
802d67d10Sthorpej  * by Jason R. Thorpe.
902d67d10Sthorpej  *
1002d67d10Sthorpej  * Redistribution and use in source and binary forms, with or without
1102d67d10Sthorpej  * modification, are permitted provided that the following conditions
1202d67d10Sthorpej  * are met:
1302d67d10Sthorpej  * 1. Redistributions of source code must retain the above copyright
1402d67d10Sthorpej  *    notice, this list of conditions and the following disclaimer.
1502d67d10Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
1602d67d10Sthorpej  *    notice, this list of conditions and the following disclaimer in the
1702d67d10Sthorpej  *    documentation and/or other materials provided with the distribution.
1802d67d10Sthorpej  *
1902d67d10Sthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2002d67d10Sthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2102d67d10Sthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2202d67d10Sthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2302d67d10Sthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2402d67d10Sthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2502d67d10Sthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2602d67d10Sthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2702d67d10Sthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2802d67d10Sthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2902d67d10Sthorpej  * POSSIBILITY OF SUCH DAMAGE.
3002d67d10Sthorpej  */
3102d67d10Sthorpej 
3202d67d10Sthorpej #if HAVE_NBTOOL_CONFIG_H
3302d67d10Sthorpej #include "nbtool_config.h"
3402d67d10Sthorpej #endif
3502d67d10Sthorpej 
3602d67d10Sthorpej #include <sys/cdefs.h>
3702d67d10Sthorpej #if !defined(__lint)
38*a4d52a8bSandvar __RCSID("$NetBSD: evboards.c,v 1.8 2022/11/01 19:51:46 andvar Exp $");
3902d67d10Sthorpej #endif  /* !__lint */
4002d67d10Sthorpej 
4102d67d10Sthorpej #include <sys/types.h>
426e45676fSthorpej #include <sys/param.h>		/* for roundup() */
4302d67d10Sthorpej #include <sys/stat.h>
4402d67d10Sthorpej #include <assert.h>
4502d67d10Sthorpej #include <err.h>
4602d67d10Sthorpej #include <errno.h>
4702d67d10Sthorpej #include <fcntl.h>
4802d67d10Sthorpej #include <fts.h>
4902d67d10Sthorpej #include <inttypes.h>
5002d67d10Sthorpej #include <limits.h>
5102d67d10Sthorpej #include <stdarg.h>
5202d67d10Sthorpej #include <stdlib.h>
5302d67d10Sthorpej #include <stdio.h>
5402d67d10Sthorpej #include <string.h>
5502d67d10Sthorpej #include <unistd.h>
5602d67d10Sthorpej 
5702d67d10Sthorpej #ifdef SUPPORT_FDT
5802d67d10Sthorpej #include "libfdt.h"
5902d67d10Sthorpej #endif
6002d67d10Sthorpej 
6102d67d10Sthorpej #if !HAVE_NBTOOL_CONFIG_H
6202d67d10Sthorpej #include <sys/utsname.h>
6302d67d10Sthorpej 
6402d67d10Sthorpej #ifdef SUPPORT_OPENFIRMWARE
6502d67d10Sthorpej #include <sys/ioctl.h>
6602d67d10Sthorpej #include <dev/ofw/openfirmio.h>
6702d67d10Sthorpej #endif
6802d67d10Sthorpej 
6902d67d10Sthorpej #endif /* ! HAVE_NBTOOL_CONFIG_H */
7002d67d10Sthorpej 
7102d67d10Sthorpej #include "installboot.h"
7202d67d10Sthorpej #include "evboards.h"
7302d67d10Sthorpej 
7402d67d10Sthorpej /*
7502d67d10Sthorpej  * The board database is implemented as a property list.  The base
7602d67d10Sthorpej  * system provides a set of known boards, keyed by their "compatible"
7702d67d10Sthorpej  * device tree property.
7802d67d10Sthorpej  *
7902d67d10Sthorpej  * The database provided by the base system is meant to help guide
8002d67d10Sthorpej  * the user as to which u-boot package needs to be installed on the
8102d67d10Sthorpej  * system in order to write the boot loader to the boot media.  The
8202d67d10Sthorpej  * base board plist is specific to the $MACHINE (e.g. "evbarm"), and
8302d67d10Sthorpej  * is installed along with the build tools, e.g.:
8402d67d10Sthorpej  *
8502d67d10Sthorpej  * (native location)
8602d67d10Sthorpej  *	/usr/sbin/installboot
8702d67d10Sthorpej  *	/usr/share/installboot/evbarm/boards.plist
8802d67d10Sthorpej  *	/usr/share/installboot/evbmips/boards.plist
8902d67d10Sthorpej  *
9002d67d10Sthorpej  * (example cross host tool location)
9102d67d10Sthorpej  *	/usr/local/xnbsd/bin/nbinstallboot
9202d67d10Sthorpej  *	/usr/local/xnbsd/share/installboot/evbarm/boards.plist
9302d67d10Sthorpej  *	/usr/local/xnbsd/share/installboot/evbmips/boards.plist
9402d67d10Sthorpej  *
9502d67d10Sthorpej  * The schema of the base board plist is as follows:
9602d67d10Sthorpej  *
9702d67d10Sthorpej  * <plist>
9802d67d10Sthorpej  * <dict>
9902d67d10Sthorpej  *	<!--
10002d67d10Sthorpej  *	  -- Key: string matching a "compatible" DT property.
10102d67d10Sthorpej  *	  -- Value: dictionary representing a board object.
10202d67d10Sthorpej  *	  -- (required)
10302d67d10Sthorpej  *	  -->
10402d67d10Sthorpej  *	<key>example,example-board</key>
10502d67d10Sthorpej  *	<dict>
10602d67d10Sthorpej  *		<!--
10702d67d10Sthorpej  *		  -- Key: "description".
10802d67d10Sthorpej  *		  -- Value: string containing the board description.
10902d67d10Sthorpej  *		  -- (required)
11002d67d10Sthorpej  *		  -->
11102d67d10Sthorpej  *		<key>description</key>
11202d67d10Sthorpej  *		<string>Example Co. Example Board</string>
11302d67d10Sthorpej  *
11402d67d10Sthorpej  *		<!--
11502d67d10Sthorpej  *		  -- Key: "u-boot-pkg".
11602d67d10Sthorpej  *		  -- Value: string representing the board-specific
11702d67d10Sthorpej  *		  --        portion of the u-boot package name.
11802d67d10Sthorpej  *		  --        In this example, the package's full name
11902d67d10Sthorpej  *		  --        is "u-boot-exampleboard".  This is used
12002d67d10Sthorpej  *		  --        to recommend to the user which u-boot
12102d67d10Sthorpej  *		  --        package to install.  If not present, then
12202d67d10Sthorpej  *		  --        no package recommendation will be made.
12302d67d10Sthorpej  *		  -- (optional)
12402d67d10Sthorpej  *		  -->
12502d67d10Sthorpej  *		<key>u-boot-pkg</key>
12602d67d10Sthorpej  *		<string>exampleboard</string>
12702d67d10Sthorpej  *	</dict>
12802d67d10Sthorpej  * </dict>
12902d67d10Sthorpej  * </plist>
13002d67d10Sthorpej  *
13102d67d10Sthorpej  * Individual u-boot packages install their own overlay property list
13202d67d10Sthorpej  * files that installboot(8) then scans for.  These overlay files are
13302d67d10Sthorpej  * named "installboot.plist", and are installed alongside the u-boot
13402d67d10Sthorpej  * binaries by the individual u-boot packages, for example:
13502d67d10Sthorpej  *
13602d67d10Sthorpej  *	/usr/pkg/share/u-boot/exampleboard/installboot.plist
13702d67d10Sthorpej  *	/usr/pkg/share/u-boot/exampleboard/u-boot-with-spl.bin
13802d67d10Sthorpej  *
13902d67d10Sthorpej  * installboot(8) scans a set of directories looking for "installboot.plist"
14002d67d10Sthorpej  * overlay files one directory deep.  For example:
14102d67d10Sthorpej  *
14202d67d10Sthorpej  *	/usr/pkg/share/u-boot/
14302d67d10Sthorpej  *				exampleboard/installboot.plist
14402d67d10Sthorpej  *				superarmdeluxe/installboot.plist
14502d67d10Sthorpej  *				dummy/
14602d67d10Sthorpej  *
14702d67d10Sthorpej  * In this example, "/usr/pkg/share/u-boot" is scanned, it would identify
14802d67d10Sthorpej  * "exampleboard" and "superarmdeluxe" as directories containing overlays
14902d67d10Sthorpej  * and load them.
15002d67d10Sthorpej  *
15102d67d10Sthorpej  * The default path scanned for u-boot packages is:
15202d67d10Sthorpej  *
15302d67d10Sthorpej  *	/usr/pkg/share/u-boot
15402d67d10Sthorpej  *
155317b3a69Sbrook  * This can be overridden with either the INSTALLBOOT_UBOOT_PATHS
156317b3a69Sbrook  * environment variable or the command line option -u, which contains
157317b3a69Sbrook  * a colon-separated list of directories, e.g.:
15802d67d10Sthorpej  *
15902d67d10Sthorpej  *	/usr/pkg/share/u-boot:/home/jmcneill/hackityhack/u-boot
16002d67d10Sthorpej  *
16102d67d10Sthorpej  * The scan only consults the top-level children of the specified directory.
16202d67d10Sthorpej  *
16302d67d10Sthorpej  * Each overlay includes complete board objects that entirely replace
16402d67d10Sthorpej  * the system-provided board objects in memory.  Some of the keys in
16502d67d10Sthorpej  * overlay board objects are computed at run-time and should not appear
16602d67d10Sthorpej  * in the plists loaded from the file system.
16702d67d10Sthorpej  *
16802d67d10Sthorpej  * The schema of the overlay board plists are as follows:
16902d67d10Sthorpej  *
17002d67d10Sthorpej  * <plist>
17102d67d10Sthorpej  * <dict>
17202d67d10Sthorpej  *	<!--
17302d67d10Sthorpej  *	  -- Key: string matching a "compatible" DT property.
17402d67d10Sthorpej  *	  -- Value: dictionary representing a board object.
17502d67d10Sthorpej  *	  -- (required)
17602d67d10Sthorpej  *	  -->
17702d67d10Sthorpej  *	<key>example,example-board</key>
17802d67d10Sthorpej  *	<dict>
17902d67d10Sthorpej  *		<!--
18002d67d10Sthorpej  *		  -- Key: "description".
18102d67d10Sthorpej  *		  -- Value: string containing the board description.
18202d67d10Sthorpej  *		  -- (required)
18302d67d10Sthorpej  *		  -->
18402d67d10Sthorpej  *		<key>description</key>
18502d67d10Sthorpej  *		<string>Example Co. Example Board</string>
18602d67d10Sthorpej  *
18702d67d10Sthorpej  *		<!--
18802d67d10Sthorpej  *		  -- Key: "u-boot-install".
18902d67d10Sthorpej  *		  --      (and variants; see discussion below)
19002d67d10Sthorpej  *		  --      "u-boot-install-emmc", etc.).
19102d67d10Sthorpej  *		  -- Value: Array of u-boot installation step objects,
19202d67d10Sthorpej  *		  --        as described below.
19302d67d10Sthorpej  *		  -- (required)
19402d67d10Sthorpej  *		  --
19502d67d10Sthorpej  *		  -- At least one of these objects is required.  If the
19602d67d10Sthorpej  *		  -- board uses a single set of steps for all boot media
19702d67d10Sthorpej  *		  -- types, then it should provide just "u-boot-install".
198ba70c588Sandvar  *		  -- Otherwise, it would provide one or more objects
19902d67d10Sthorpej  *		  -- with names reflecting the media type, e.g.:
20002d67d10Sthorpej  *		  --
20102d67d10Sthorpej  *		  --	"u-boot-install-sdmmc"	(for SD cards)
20202d67d10Sthorpej  *		  --	"u-boot-install-emmc"	(for eMMC modules)
20302d67d10Sthorpej  *		  --	"u-boot-install-usb"	(for USB block storage)
2046e45676fSthorpej  *		  --	"u-boot-install-spi"	(for SPI NOR flash)
20502d67d10Sthorpej  *		  --
20602d67d10Sthorpej  *		  -- These installation steps will be selectable using
20702d67d10Sthorpej  *		  -- the "media=..." option to installboot(8).
20802d67d10Sthorpej  *		  -->
20902d67d10Sthorpej  *		<key>u-boot-install</key>
21002d67d10Sthorpej  *		<array>
21102d67d10Sthorpej  *			<!-- see installation object discussion below. -->
21202d67d10Sthorpej  *		</array>
21302d67d10Sthorpej  *
21402d67d10Sthorpej  *		<!--
21502d67d10Sthorpej  *		  -- Key: "runtime-u-boot-path"
21602d67d10Sthorpej  *		  -- Value: A string representing the path to the u-boot
21702d67d10Sthorpej  *		  --        binary files needed to install the boot loader.
21802d67d10Sthorpej  *		  --        This value is computed at run-time and is the
219*a4d52a8bSandvar  *		  --        same directory in which the installboot.plist
22002d67d10Sthorpej  *		  --        file for that u-boot package is located.
22102d67d10Sthorpej  *		  --        This key/value pair should never be included
22202d67d10Sthorpej  *		  --        in an installboot.plist file, and including it
22302d67d10Sthorpej  *		  --	    will cause the overlay to be rejected.
22402d67d10Sthorpej  *		  -- (computed at run-time)
22502d67d10Sthorpej  *		  -->
22602d67d10Sthorpej  *		<key>runtime-u-boot-path</key>
22702d67d10Sthorpej  *		<string>/usr/pkg/share/u-boot/exampleboard</string>
22802d67d10Sthorpej  *	</dict>
22902d67d10Sthorpej  * </dict>
23002d67d10Sthorpej  * </plist>
23102d67d10Sthorpej  *
23202d67d10Sthorpej  * The installation objects provide a description of the steps needed
23302d67d10Sthorpej  * to install u-boot on the boot media.  Each installation object it
23402d67d10Sthorpej  * itself an array of step object.
23502d67d10Sthorpej  *
23602d67d10Sthorpej  * A basic installation object has a single step that instructs
23702d67d10Sthorpej  * installboot(8) to write a file to a specific offset onto the
23802d67d10Sthorpej  * boot media.
23902d67d10Sthorpej  *
24002d67d10Sthorpej  *	<key>u-boot-install</key>
24102d67d10Sthorpej  *	<!-- installation object -->
24202d67d10Sthorpej  *	<array>
24302d67d10Sthorpej  *		<!-- step object -->
24402d67d10Sthorpej  *		<dict>
24502d67d10Sthorpej  *			<!--
24602d67d10Sthorpej  *			  -- Key: "file-name".
24702d67d10Sthorpej  *			  -- Value: a string naming the file to be
24802d67d10Sthorpej  *			  --        written to the media.
24902d67d10Sthorpej  *			  -- (required)
25002d67d10Sthorpej  *			  -->
25102d67d10Sthorpej  *			<key>file-name</key>
25202d67d10Sthorpej  *			<string>u-boot-with-spl.bin</string>
25302d67d10Sthorpej  *
25402d67d10Sthorpej  *			<!--
25502d67d10Sthorpej  *			  -- Key: "image-offset".
25602d67d10Sthorpej  *			  -- Value: an integer specifying the offset
25702d67d10Sthorpej  *			  --        into the output image or device
25802d67d10Sthorpej  *			  --        where to write the file.  Defaults
25902d67d10Sthorpej  *			  --        to 0 if not specified.
26002d67d10Sthorpej  *			  -- (optional)
26102d67d10Sthorpej  *			  -->
26202d67d10Sthorpej  *			<key>image-offset</key>
26302d67d10Sthorpej  *			<integer>8192</integer>
26402d67d10Sthorpej  *		</dict>
26502d67d10Sthorpej  *	</array>
26602d67d10Sthorpej  *
26702d67d10Sthorpej  * Some installations require multiple steps with special handling.
26802d67d10Sthorpej  *
26902d67d10Sthorpej  *	<key>u-boot-install</key>
27002d67d10Sthorpej  *	<array>
27102d67d10Sthorpej  *		<--
27202d67d10Sthorpej  *		 -- Step 1: Write the initial portion of the boot
27302d67d10Sthorpej  *		 -- loader onto the media.  The loader has a "hole"
27402d67d10Sthorpej  *		 -- to leave room for the MBR partition table.  Take
27502d67d10Sthorpej  *		 -- care not to scribble over the table.
27602d67d10Sthorpej  *		 -->
27702d67d10Sthorpej  *		<dict>
27802d67d10Sthorpej  *			<key>file-name</key>
27902d67d10Sthorpej  *			<string>u-boot-img.bin</string>
28002d67d10Sthorpej  *
28102d67d10Sthorpej  *			<!--
28202d67d10Sthorpej  *			  -- Key: "file-size".
28302d67d10Sthorpej  *			  -- Value: an integer specifying the amount of
28402d67d10Sthorpej  *			  --        data from the file to be written to the
28502d67d10Sthorpej  *			  --        output.  Defaults to "to end of file" if
28602d67d10Sthorpej  *			  --        not specified.
28702d67d10Sthorpej  *			  -- (optional)
28802d67d10Sthorpej  *			  -->
28902d67d10Sthorpej  *			<!-- Stop short of the MBR partition table. -->
29002d67d10Sthorpej  *			<key>file-size</key>
29102d67d10Sthorpej  *			<integer>442</integer>
29202d67d10Sthorpej  *
29302d67d10Sthorpej  *			<!--
29402d67d10Sthorpej  *			  -- Key: "preserve".
29502d67d10Sthorpej  *			  -- Value: a boolean indicating that any partial
29602d67d10Sthorpej  *			  --        output block should preserve any pre-
29702d67d10Sthorpej  *			  --        existing contents of that block for
29802d67d10Sthorpej  *			  --        the portion of the of the block not
29902d67d10Sthorpej  *			  --        overwritten by the input file.
30002d67d10Sthorpej  *			  --        (read-modify-write)
30102d67d10Sthorpej  *			  -- (optional)
30202d67d10Sthorpej  *			  -->
30302d67d10Sthorpej  *			<!-- Preserve the MBR partition table. -->
30402d67d10Sthorpej  *			<key>preserve</key>
30502d67d10Sthorpej  *			<true/>
30602d67d10Sthorpej  *		</dict>
30702d67d10Sthorpej  *		<--
30802d67d10Sthorpej  *		 -- Step 2: Write the rest of the loader after the
30902d67d10Sthorpej  *		 -- MBR partition table.
31002d67d10Sthorpej  *		 -->
31102d67d10Sthorpej  *		<dict>
31202d67d10Sthorpej  *			<key>file-name</key>
31302d67d10Sthorpej  *			<string>u-boot-img.bin</string>
31402d67d10Sthorpej  *
31502d67d10Sthorpej  *			<!--
31602d67d10Sthorpej  *			  -- Key: "file-offset".
31702d67d10Sthorpej  *			  -- Value: an integer specifying the offset into
31802d67d10Sthorpej  *			  --        the input file from where to start
31902d67d10Sthorpej  *			  --        copying to the output.
32002d67d10Sthorpej  *			  -- (optional)
32102d67d10Sthorpej  *			  -->
32202d67d10Sthorpej  *			<key>file-offset</key>
32302d67d10Sthorpej  *			<integer>512</integer>
32402d67d10Sthorpej  *
325ba70c588Sandvar  *			<!-- ...just after the MBR partition table. -->
32602d67d10Sthorpej  *			<key>image-offset</key>
32702d67d10Sthorpej  *			<integer>512</integer>
32802d67d10Sthorpej  *		</dict>
32902d67d10Sthorpej  *	</array>
3306e45676fSthorpej  *
331ba70c588Sandvar  * There are some additional directives for installing on raw flash devices:
3326e45676fSthorpej  *
3336e45676fSthorpej  *	<key>u-boot-install-spi</key>
3346e45676fSthorpej  *	<array>
3356e45676fSthorpej  *		<!-- This board's SPI NOR flash is 16Mbit (2MB) in size,
3366e45676fSthorpej  *		  -- arranged as 32 512Kbit (64KB) blocks.
3376e45676fSthorpej  *		<dict>
3386e45676fSthorpej  *			<key>file-name</key>
3396e45676fSthorpej  *			<string>u-boot-with-spl.bin</string>
3406e45676fSthorpej  *
3416e45676fSthorpej  *			<!-- Key: "input-block-size".
3426e45676fSthorpej  *			  -- Value: an integer specifying how much file
3436e45676fSthorpej  *			  --        data to read per input block before
3446e45676fSthorpej  *			  --        padding.  Must be used in conjunction
3456e45676fSthorpej  *			  --        with "input-pad-size".
3466e45676fSthorpej  *			  -- (optional)
3476e45676fSthorpej  *			  -->
3486e45676fSthorpej  *			<key>input-block-size</key>
3496e45676fSthorpej  *			<integer>2048</integer>
3506e45676fSthorpej  *
3516e45676fSthorpej  *			<!-- Key: "input-pad-size".
352ba70c588Sandvar  *			  -- Value: an integer specifying the amount of
3536e45676fSthorpej  *			  --        zero padding inserted per input block.
354ba70c588Sandvar  *			  --        Must be used in conjunction with
3556e45676fSthorpej  *			  --        "input-block-size".
3566e45676fSthorpej  *			  -- (optional)
3576e45676fSthorpej  *			  -->
3586e45676fSthorpej  *			<key>input-pad-size</key>
3596e45676fSthorpej  *			<integer>2048</integer>
3606e45676fSthorpej  *
3616e45676fSthorpej  *			<!-- Key: "output-size".
3626e45676fSthorpej  *			  -- Value: an integer specifying the total
3636e45676fSthorpej  *			  --        size to be written to the output
3646e45676fSthorpej  *			  --        device.  This is used when writing
3656e45676fSthorpej  *			  --        a bootloader to a raw flash memory
3666e45676fSthorpej  *			  --        device such as a SPI NOR flash.
3676e45676fSthorpej  *			  --        The boot loader MUST fit within
3686e45676fSthorpej  *			  --        this size and the output will be
3696e45676fSthorpej  *			  --        padded to this size with zeros.
3706e45676fSthorpej  *			  --
3716e45676fSthorpej  *			  --        If the "output-block-size" key (below)
3726e45676fSthorpej  *			  --        is also specified, then this value
3736e45676fSthorpej  *			  --        must be a multiple of the output block
3746e45676fSthorpej  *			  --        size.
3756e45676fSthorpej  *			  -- (optional)
3766e45676fSthorpej  *			  -->
3776e45676fSthorpej  *			<key>output-size</key>
3786e45676fSthorpej  *			<integer>2097152</integer>
3796e45676fSthorpej  *
3806e45676fSthorpej  *			<-- Key: "output-block-size"
3816e45676fSthorpej  *			 -- Value: an integer specifying the size of
3826e45676fSthorpej  *			 --        the blocks used to write to the
3836e45676fSthorpej  *			 --        output device.  If the output device
3846e45676fSthorpej  *			 --        simulates a disk block storage device,
3856e45676fSthorpej  *			 --        then this value must be a multiple of
3866e45676fSthorpej  *			 --        the reported sector size.
3876e45676fSthorpej  *			 -- (optional)
3886e45676fSthorpej  *			 -->
3896e45676fSthorpej  *			<key>output-block-size</key>
3906e45676fSthorpej  *			<integer>65536</integer>
3916e45676fSthorpej  *		</dict>
3926e45676fSthorpej  *	</array>
3936e45676fSthorpej  *
3946e45676fSthorpej  * For boards that require a media specification to be provided, it
3956e45676fSthorpej  * may be the case that two media types have identical steps.  It
3966e45676fSthorpej  * could be confusing for users to see a list of media types that does
3976e45676fSthorpej  * not include the media type on which they are installing, so there
3986e45676fSthorpej  * is an alias capability:
3996e45676fSthorpej  *
4006e45676fSthorpej  *	<key>u-boot-install-spi</key>
4016e45676fSthorpej  *	<array>
4026e45676fSthorpej  *		.
4036e45676fSthorpej  *		.
4046e45676fSthorpej  *		.
4056e45676fSthorpej  *	</array>
4066e45676fSthorpej  *	<key>u-boot-install-sdmmc</key>
4076e45676fSthorpej  *	<array>
4086e45676fSthorpej  *		.
4096e45676fSthorpej  *		.
4106e45676fSthorpej  *		.
4116e45676fSthorpej  *	</array>
4126e45676fSthorpej  *	<-- Steps for eMMC are identical to SDMMC on this board. -->
4136e45676fSthorpej  *	<key>u-boot-install-emmc</key>
4146e45676fSthorpej  *	<string>u-boot-install-sdmmc</string>
41502d67d10Sthorpej  */
41602d67d10Sthorpej 
41702d67d10Sthorpej /*
41802d67d10Sthorpej  * make_path --
41902d67d10Sthorpej  *	Build a path into the given buffer with the specified
42002d67d10Sthorpej  *	format.  Returns NULL if the path won't fit.
42102d67d10Sthorpej  */
4223544b823Smaya static __printflike(3,4) const char *
make_path(char * buf,size_t bufsize,const char * fmt,...)42302d67d10Sthorpej make_path(char *buf, size_t bufsize, const char *fmt, ...)
42402d67d10Sthorpej {
42502d67d10Sthorpej 	va_list ap;
42602d67d10Sthorpej 	int ret;
42702d67d10Sthorpej 
42802d67d10Sthorpej 	va_start(ap, fmt);
42902d67d10Sthorpej 	ret = vsnprintf(buf, bufsize, fmt, ap);
43002d67d10Sthorpej 	va_end(ap);
43102d67d10Sthorpej 
43202d67d10Sthorpej 	if (ret < 0 || (size_t)ret >= bufsize)
43302d67d10Sthorpej 		return NULL;
43402d67d10Sthorpej 
43502d67d10Sthorpej 	return buf;
43602d67d10Sthorpej }
43702d67d10Sthorpej 
43802d67d10Sthorpej #ifndef EVBOARDS_PLIST_BASE
43902d67d10Sthorpej #define	EVBOARDS_PLIST_BASE	"/usr"
44002d67d10Sthorpej #endif
44102d67d10Sthorpej 
44202d67d10Sthorpej static const char evb_db_base_location[] =
44302d67d10Sthorpej     EVBOARDS_PLIST_BASE "/share/installboot";
44402d67d10Sthorpej 
44502d67d10Sthorpej /*
44602d67d10Sthorpej  * evb_db_base_path --
44702d67d10Sthorpej  *	Returns the path to the base board db file.
44802d67d10Sthorpej  */
44902d67d10Sthorpej static const char *
evb_db_base_path(ib_params * params,char * buf,size_t bufsize)45002d67d10Sthorpej evb_db_base_path(ib_params *params, char *buf, size_t bufsize)
45102d67d10Sthorpej {
45202d67d10Sthorpej 
45302d67d10Sthorpej 	return make_path(buf, bufsize, "%s/%s/boards.plist",
45402d67d10Sthorpej 	    evb_db_base_location, params->machine->name);
45502d67d10Sthorpej }
45602d67d10Sthorpej 
45702d67d10Sthorpej /*
45802d67d10Sthorpej  * evb_uboot_pkg_paths --
45902d67d10Sthorpej  *	Returns an array of u-boot package paths to scan for
46002d67d10Sthorpej  *	installboot.plist files.
46102d67d10Sthorpej  *
46202d67d10Sthorpej  *	Number of array elements, not including the NULL terminator,
46302d67d10Sthorpej  *	is returned in *countp.
46402d67d10Sthorpej  *
46502d67d10Sthorpej  *	The working buffer is returned in *bufp so that the caller
46602d67d10Sthorpej  *	can free it.
46702d67d10Sthorpej  */
46802d67d10Sthorpej static char **
evb_uboot_pkg_paths(ib_params * params,int * countp,void ** bufp)46902d67d10Sthorpej evb_uboot_pkg_paths(ib_params *params, int *countp, void **bufp)
47002d67d10Sthorpej {
47102d67d10Sthorpej 	char **ret_array = NULL;
47202d67d10Sthorpej 	char *buf = NULL;
47302d67d10Sthorpej 	const char *pathspec;
47402d67d10Sthorpej 	int i, count;
4758012ca3fSmsaitoh 	char *cp, *startcp;
47602d67d10Sthorpej 
477317b3a69Sbrook 	pathspec = params->uboot_paths;
478317b3a69Sbrook 	assert(pathspec != NULL);
47902d67d10Sthorpej 
48002d67d10Sthorpej 	if (strlen(pathspec) == 0)
48102d67d10Sthorpej 		goto out;
48202d67d10Sthorpej 
48302d67d10Sthorpej 	/* Count the path elements. */
48402d67d10Sthorpej 	for (cp = __UNCONST(pathspec), count = 0;;) {
48502d67d10Sthorpej 		count++;
48602d67d10Sthorpej 		cp = strchr(cp, ':');
48702d67d10Sthorpej 		if (cp == NULL)
48802d67d10Sthorpej 			break;
48902d67d10Sthorpej 		cp++;
49002d67d10Sthorpej 	}
49102d67d10Sthorpej 
49202d67d10Sthorpej 	buf = malloc((sizeof(char *) * (count + 1)) +
49302d67d10Sthorpej 		     strlen(pathspec) + 1);
49402d67d10Sthorpej 	if (buf == NULL)
49502d67d10Sthorpej 		goto out;
49602d67d10Sthorpej 
49702d67d10Sthorpej 	/*
49802d67d10Sthorpej 	 * Because we want to follow the usual "paths are listed in priority
49902d67d10Sthorpej 	 * order" semantics, we reverse the order of the paths when we put
50002d67d10Sthorpej 	 * them into the array we feed to fts.  This is because we always
50102d67d10Sthorpej 	 * overwrite existing entries as we find them, thus the last board
50202d67d10Sthorpej 	 * object found one a given key is the one that will be used.
50302d67d10Sthorpej 	 */
50402d67d10Sthorpej 
50502d67d10Sthorpej 	ret_array = (char **)buf;
50602d67d10Sthorpej 	startcp = buf + (sizeof(char *) * (count + 1));
50702d67d10Sthorpej 	/* this is a safe strcpy(); don't replace it. */
50802d67d10Sthorpej 	strcpy(startcp, pathspec);
50902d67d10Sthorpej 
51002d67d10Sthorpej 	cp = strrchr(startcp, ':');
51102d67d10Sthorpej 	if (cp == NULL)
51202d67d10Sthorpej 		cp = startcp;
51302d67d10Sthorpej 
51402d67d10Sthorpej 	for (i = 0;;) {
51502d67d10Sthorpej 		if (*cp == ':') {
51602d67d10Sthorpej 			ret_array[i++] = cp+1;
51702d67d10Sthorpej 			*cp-- = '\0';
51802d67d10Sthorpej 		} else
51902d67d10Sthorpej 			ret_array[i++] = cp;
52002d67d10Sthorpej 		if (cp == startcp)
52102d67d10Sthorpej 			break;
52202d67d10Sthorpej 		cp = strrchr(cp, ':');
52302d67d10Sthorpej 		if (cp == NULL)
52402d67d10Sthorpej 			cp = startcp;
52502d67d10Sthorpej 	}
52602d67d10Sthorpej 	assert(i == count);
52702d67d10Sthorpej 	ret_array[i] = NULL;
52802d67d10Sthorpej 
52902d67d10Sthorpej  out:
53002d67d10Sthorpej 	if (ret_array == NULL) {
53102d67d10Sthorpej 		if (buf != NULL)
53202d67d10Sthorpej 			free(buf);
53302d67d10Sthorpej 	} else {
53402d67d10Sthorpej 		if (countp != NULL)
53502d67d10Sthorpej 			*countp = count;
53602d67d10Sthorpej 		if (bufp != NULL)
53702d67d10Sthorpej 			*bufp = buf;
53802d67d10Sthorpej 	}
53902d67d10Sthorpej 	return ret_array;
54002d67d10Sthorpej }
54102d67d10Sthorpej 
54202d67d10Sthorpej static const char step_file_name_key[] = "file-name";
54302d67d10Sthorpej static const char step_file_offset_key[] = "file-offset";
54402d67d10Sthorpej static const char step_file_size_key[] = "file-size";
54502d67d10Sthorpej static const char step_image_offset_key[] = "image-offset";
5466e45676fSthorpej static const char step_input_block_size_key[] = "input-block-size";
5476e45676fSthorpej static const char step_input_pad_size_key[] = "input-pad-size";
5486e45676fSthorpej static const char step_output_size_key[] = "output-size";
5496e45676fSthorpej static const char step_output_block_size_key[] = "output-block-size";
55002d67d10Sthorpej static const char step_preserve_key[] = "preserve";
55102d67d10Sthorpej 
55202d67d10Sthorpej static bool
validate_ubstep_object(evb_ubstep obj)55302d67d10Sthorpej validate_ubstep_object(evb_ubstep obj)
55402d67d10Sthorpej {
55502d67d10Sthorpej 	/*
55602d67d10Sthorpej 	 * evb_ubstep is a dictionary with the following keys:
55702d67d10Sthorpej 	 *
55802d67d10Sthorpej 	 *	"file-name"         (string) (required)
55902d67d10Sthorpej 	 *	"file-offset"       (number) (optional)
56002d67d10Sthorpej 	 *	"file-size"         (number) (optional)
56102d67d10Sthorpej 	 *	"image-offset"      (number) (optional)
5626e45676fSthorpej 	 *	"input-block-size"  (number) (optional)
5636e45676fSthorpej 	 *	"input-pad-size"    (number) (optional)
5646e45676fSthorpej 	 *	"output-size"       (number) (optional)
5656e45676fSthorpej 	 *	"output-block-size" (number) (optional)
56602d67d10Sthorpej 	 *	"preserve"          (bool)   (optional)
56702d67d10Sthorpej 	 */
56802d67d10Sthorpej 	if (prop_object_type(obj) != PROP_TYPE_DICTIONARY)
56902d67d10Sthorpej 		return false;
57002d67d10Sthorpej 
57102d67d10Sthorpej 	prop_object_t v;
57202d67d10Sthorpej 
57302d67d10Sthorpej 	v = prop_dictionary_get(obj, step_file_name_key);
57402d67d10Sthorpej 	if (v == NULL ||
57502d67d10Sthorpej 	    prop_object_type(v) != PROP_TYPE_STRING)
57602d67d10Sthorpej 	    	return false;
57702d67d10Sthorpej 
57802d67d10Sthorpej 	v = prop_dictionary_get(obj, step_file_offset_key);
57902d67d10Sthorpej 	if (v != NULL &&
58002d67d10Sthorpej 	    prop_object_type(v) != PROP_TYPE_NUMBER)
58102d67d10Sthorpej 	    	return false;
58202d67d10Sthorpej 
58302d67d10Sthorpej 	v = prop_dictionary_get(obj, step_file_size_key);
58402d67d10Sthorpej 	if (v != NULL &&
58502d67d10Sthorpej 	    prop_object_type(v) != PROP_TYPE_NUMBER)
58602d67d10Sthorpej 	    	return false;
58702d67d10Sthorpej 
58802d67d10Sthorpej 	v = prop_dictionary_get(obj, step_image_offset_key);
58902d67d10Sthorpej 	if (v != NULL &&
59002d67d10Sthorpej 	    prop_object_type(v) != PROP_TYPE_NUMBER)
59102d67d10Sthorpej 	    	return false;
59202d67d10Sthorpej 
5936e45676fSthorpej 	bool have_input_block_size = false;
5946e45676fSthorpej 	bool have_input_pad_size = false;
5956e45676fSthorpej 
5966e45676fSthorpej 	v = prop_dictionary_get(obj, step_input_block_size_key);
5976e45676fSthorpej 	if (v != NULL) {
5986e45676fSthorpej 		have_input_block_size = true;
5996e45676fSthorpej 		if (prop_object_type(v) != PROP_TYPE_NUMBER)
6006e45676fSthorpej 			return false;
6016e45676fSthorpej 	}
6026e45676fSthorpej 
6036e45676fSthorpej 	v = prop_dictionary_get(obj, step_input_pad_size_key);
6046e45676fSthorpej 	if (v != NULL) {
6056e45676fSthorpej 		have_input_pad_size = true;
6066e45676fSthorpej 		if (prop_object_type(v) != PROP_TYPE_NUMBER)
6076e45676fSthorpej 			return false;
6086e45676fSthorpej 	}
6096e45676fSthorpej 
6106e45676fSthorpej 	/* Must have both or neither of input-{block,pad}-size. */
6116e45676fSthorpej 	if (have_input_block_size ^ have_input_pad_size)
6126e45676fSthorpej 		return false;
6136e45676fSthorpej 
6146e45676fSthorpej 	v = prop_dictionary_get(obj, step_output_size_key);
6156e45676fSthorpej 	if (v != NULL &&
6166e45676fSthorpej 	    prop_object_type(v) != PROP_TYPE_NUMBER)
6176e45676fSthorpej 		return false;
6186e45676fSthorpej 
6196e45676fSthorpej 	v = prop_dictionary_get(obj, step_output_block_size_key);
6206e45676fSthorpej 	if (v != NULL &&
6216e45676fSthorpej 	    prop_object_type(v) != PROP_TYPE_NUMBER)
6226e45676fSthorpej 		return false;
6236e45676fSthorpej 
62402d67d10Sthorpej 	v = prop_dictionary_get(obj, step_preserve_key);
62502d67d10Sthorpej 	if (v != NULL &&
62602d67d10Sthorpej 	    prop_object_type(v) != PROP_TYPE_BOOL)
62702d67d10Sthorpej 	    	return false;
62802d67d10Sthorpej 
62902d67d10Sthorpej 	return true;
63002d67d10Sthorpej }
63102d67d10Sthorpej 
63202d67d10Sthorpej static bool
validate_ubinstall_object(evb_board board,evb_ubinstall obj)6336e45676fSthorpej validate_ubinstall_object(evb_board board, evb_ubinstall obj)
63402d67d10Sthorpej {
63502d67d10Sthorpej 	/*
6366e45676fSthorpej 	 * evb_ubinstall is either:
6376e45676fSthorpej 	 * -- an array with one or more evb_ubstep objects.
6386e45676fSthorpej 	 * -- a string representing an alias of another evb_ubinstall
6396e45676fSthorpej 	 *    object
64002d67d10Sthorpej 	 *
64102d67d10Sthorpej 	 * (evb_ubsteps is just a convenience type for iterating
64202d67d10Sthorpej 	 * over the steps.)
64302d67d10Sthorpej 	 */
6446e45676fSthorpej 
6456e45676fSthorpej 	if (prop_object_type(obj) == PROP_TYPE_STRING) {
6466e45676fSthorpej 		evb_ubinstall tobj = prop_dictionary_get(board,
647bba3c683Sthorpej 		    prop_string_value((prop_string_t)obj));
6486e45676fSthorpej 
6496e45676fSthorpej 		/*
6506e45676fSthorpej 		 * The target evb_ubinstall object must exist
6516e45676fSthorpej 		 * and must itself be a proper evb_ubinstall,
6526e45676fSthorpej 		 * not another alias.
6536e45676fSthorpej 		 */
6546e45676fSthorpej 		if (tobj == NULL ||
6556e45676fSthorpej 		    prop_object_type(tobj) != PROP_TYPE_ARRAY) {
6566e45676fSthorpej 			return false;
6576e45676fSthorpej 		}
6586e45676fSthorpej 		return true;
6596e45676fSthorpej 	}
6606e45676fSthorpej 
66102d67d10Sthorpej 	if (prop_object_type(obj) != PROP_TYPE_ARRAY)
66202d67d10Sthorpej 		return false;
66302d67d10Sthorpej 	if (prop_array_count(obj) < 1)
66402d67d10Sthorpej 		return false;
66502d67d10Sthorpej 
66602d67d10Sthorpej 	prop_object_t v;
66702d67d10Sthorpej 	prop_object_iterator_t iter = prop_array_iterator(obj);
66802d67d10Sthorpej 
66902d67d10Sthorpej 	while ((v = prop_object_iterator_next(iter)) != NULL) {
67002d67d10Sthorpej 		if (!validate_ubstep_object(v))
67102d67d10Sthorpej 			break;
67202d67d10Sthorpej 	}
67302d67d10Sthorpej 
67402d67d10Sthorpej 	prop_object_iterator_release(iter);
67502d67d10Sthorpej 	return v == NULL;
67602d67d10Sthorpej }
67702d67d10Sthorpej 
67802d67d10Sthorpej static const char board_description_key[] = "description";
67902d67d10Sthorpej static const char board_u_boot_pkg_key[] = "u-boot-pkg";
68002d67d10Sthorpej static const char board_u_boot_path_key[] = "runtime-u-boot-path";
68102d67d10Sthorpej static const char board_u_boot_install_key[] = "u-boot-install";
68202d67d10Sthorpej 
68302d67d10Sthorpej static bool
validate_board_object(evb_board obj,bool is_overlay)68402d67d10Sthorpej validate_board_object(evb_board obj, bool is_overlay)
68502d67d10Sthorpej {
68602d67d10Sthorpej 	/*
68702d67d10Sthorpej 	 * evb_board is a dictionary with the following keys:
68802d67d10Sthorpej 	 *
68902d67d10Sthorpej 	 *	"description"		(string) (required)
69002d67d10Sthorpej 	 *	"u-boot-pkg"		(string) (optional, base only)
69102d67d10Sthorpej 	 *	"runtime-u-boot-path"	(string) (required, overlay only)
69202d67d10Sthorpej 	 *
69302d67d10Sthorpej 	 * With special consideration for these keys:
69402d67d10Sthorpej 	 *
69502d67d10Sthorpej 	 * Either this key and no other "u-boot-install*" keys:
69602d67d10Sthorpej 	 *	"u-boot-install"	(string) (required, overlay only)
69702d67d10Sthorpej 	 *
69802d67d10Sthorpej 	 * Or one or more keys of the following pattern:
69902d67d10Sthorpej 	 *	"u-boot-install-*"	(string) (required, overlay only)
70002d67d10Sthorpej 	 */
70102d67d10Sthorpej 	bool has_default_install = false;
70202d67d10Sthorpej 	bool has_media_install = false;
70302d67d10Sthorpej 
70402d67d10Sthorpej 	if (prop_object_type(obj) != PROP_TYPE_DICTIONARY)
70502d67d10Sthorpej 		return false;
70602d67d10Sthorpej 
70702d67d10Sthorpej 	prop_object_t v;
70802d67d10Sthorpej 
70902d67d10Sthorpej 	v = prop_dictionary_get(obj, board_description_key);
71002d67d10Sthorpej 	if (v == NULL ||
71102d67d10Sthorpej 	    prop_object_type(v) != PROP_TYPE_STRING)
71202d67d10Sthorpej 	    	return false;
71302d67d10Sthorpej 
71402d67d10Sthorpej 	v = prop_dictionary_get(obj, board_u_boot_pkg_key);
71502d67d10Sthorpej 	if (v != NULL &&
71602d67d10Sthorpej 	    (is_overlay || prop_object_type(v) != PROP_TYPE_STRING))
71702d67d10Sthorpej 	    	return false;
71802d67d10Sthorpej 
71902d67d10Sthorpej 	/*
72002d67d10Sthorpej 	 * "runtime-u-boot-path" is added to an overlay after we've
72102d67d10Sthorpej 	 * validated the board object, so simply make sure it's not
72202d67d10Sthorpej 	 * present.
72302d67d10Sthorpej 	 */
72402d67d10Sthorpej 	v = prop_dictionary_get(obj, board_u_boot_path_key);
72502d67d10Sthorpej 	if (v != NULL)
72602d67d10Sthorpej 		return false;
72702d67d10Sthorpej 
72802d67d10Sthorpej 	prop_object_iterator_t iter = prop_dictionary_iterator(obj);
72902d67d10Sthorpej 	prop_dictionary_keysym_t key;
73002d67d10Sthorpej 	while ((key = prop_object_iterator_next(iter)) != NULL) {
731bba3c683Sthorpej 		const char *cp = prop_dictionary_keysym_value(key);
73202d67d10Sthorpej 		if (strcmp(cp, board_u_boot_install_key) == 0) {
73302d67d10Sthorpej 			has_default_install = true;
73402d67d10Sthorpej 		} else if (strncmp(cp, board_u_boot_install_key,
73502d67d10Sthorpej 				   sizeof(board_u_boot_install_key) - 1) == 0 &&
73602d67d10Sthorpej 			   cp[sizeof(board_u_boot_install_key) - 1] == '-') {
73702d67d10Sthorpej 			has_media_install = true;
73802d67d10Sthorpej 		} else {
73902d67d10Sthorpej 			continue;
74002d67d10Sthorpej 		}
74102d67d10Sthorpej 		v = prop_dictionary_get_keysym(obj, key);
74202d67d10Sthorpej 		assert(v != NULL);
7436e45676fSthorpej 		if (!is_overlay || !validate_ubinstall_object(obj, v))
74402d67d10Sthorpej 			break;
74502d67d10Sthorpej 	}
74602d67d10Sthorpej 	prop_object_iterator_release(iter);
74702d67d10Sthorpej 	if (key != NULL)
74802d67d10Sthorpej 		return false;
74902d67d10Sthorpej 
75002d67d10Sthorpej 	/*
75102d67d10Sthorpej 	 * Overlays must have only a default install key OR one or more
75202d67d10Sthorpej 	 * media install keys.
75302d67d10Sthorpej 	 */
75402d67d10Sthorpej 	if (is_overlay)
75502d67d10Sthorpej 		return has_default_install ^ has_media_install;
75602d67d10Sthorpej 
75702d67d10Sthorpej 	/*
75802d67d10Sthorpej 	 * Base board objects must have neither.
75902d67d10Sthorpej 	 */
76002d67d10Sthorpej 	return (has_default_install | has_media_install) == false;
76102d67d10Sthorpej }
76202d67d10Sthorpej 
76302d67d10Sthorpej /*
76402d67d10Sthorpej  * evb_db_load_overlay --
76502d67d10Sthorpej  *	Load boards from an overlay file into the db.
76602d67d10Sthorpej  */
76702d67d10Sthorpej static void
evb_db_load_overlay(ib_params * params,const char * path,const char * runtime_uboot_path)76802d67d10Sthorpej evb_db_load_overlay(ib_params *params, const char *path,
76902d67d10Sthorpej     const char *runtime_uboot_path)
77002d67d10Sthorpej {
77102d67d10Sthorpej 	prop_dictionary_t overlay;
77202d67d10Sthorpej 	struct stat sb;
77302d67d10Sthorpej 
77402d67d10Sthorpej 	if (params->flags & IB_VERBOSE)
77502d67d10Sthorpej 		printf("Loading '%s'.\n", path);
77602d67d10Sthorpej 
77702d67d10Sthorpej 	if (stat(path, &sb) < 0) {
77802d67d10Sthorpej 		warn("'%s'", path);
77902d67d10Sthorpej 		return;
78002d67d10Sthorpej 	} else {
78102d67d10Sthorpej 		overlay = prop_dictionary_internalize_from_file(path);
78202d67d10Sthorpej 		if (overlay == NULL) {
78302d67d10Sthorpej 			warnx("unable to parse overlay '%s'", path);
78402d67d10Sthorpej 			return;
78502d67d10Sthorpej 		}
78602d67d10Sthorpej 	}
78702d67d10Sthorpej 
78802d67d10Sthorpej 	/*
78902d67d10Sthorpej 	 * Validate all of the board objects and add them to the board
79002d67d10Sthorpej 	 * db, replacing any existing entries as we go.
79102d67d10Sthorpej 	 */
79202d67d10Sthorpej 	prop_object_iterator_t iter = prop_dictionary_iterator(overlay);
79302d67d10Sthorpej 	prop_dictionary_keysym_t key;
79402d67d10Sthorpej 	prop_dictionary_t board;
79502d67d10Sthorpej 	while ((key = prop_object_iterator_next(iter)) != NULL) {
79602d67d10Sthorpej 		board = prop_dictionary_get_keysym(overlay, key);
79702d67d10Sthorpej 		assert(board != NULL);
79802d67d10Sthorpej 		if (!validate_board_object(board, true)) {
79902d67d10Sthorpej 			warnx("invalid board object in '%s': '%s'", path,
800bba3c683Sthorpej 			    prop_dictionary_keysym_value(key));
80102d67d10Sthorpej 			continue;
80202d67d10Sthorpej 		}
80302d67d10Sthorpej 
80402d67d10Sthorpej 		/* Add "runtime-u-boot-path". */
80502d67d10Sthorpej 		prop_string_t string =
806bba3c683Sthorpej 		    prop_string_create_copy(runtime_uboot_path);
80702d67d10Sthorpej 		assert(string != NULL);
80802d67d10Sthorpej 		prop_dictionary_set(board, board_u_boot_path_key, string);
80902d67d10Sthorpej 		prop_object_release(string);
81002d67d10Sthorpej 
81102d67d10Sthorpej 		/* Insert into board db. */
81202d67d10Sthorpej 		prop_dictionary_set_keysym(params->mach_data, key, board);
81302d67d10Sthorpej 	}
81402d67d10Sthorpej 	prop_object_iterator_release(iter);
81502d67d10Sthorpej 	prop_object_release(overlay);
81602d67d10Sthorpej }
81702d67d10Sthorpej 
81802d67d10Sthorpej /*
81902d67d10Sthorpej  * evb_db_load_overlays --
82002d67d10Sthorpej  *	Load the overlays from the search path.
82102d67d10Sthorpej  */
82202d67d10Sthorpej static void
evb_db_load_overlays(ib_params * params)82302d67d10Sthorpej evb_db_load_overlays(ib_params *params)
82402d67d10Sthorpej {
82502d67d10Sthorpej 	char overlay_pathbuf[PATH_MAX+1];
82602d67d10Sthorpej 	const char *overlay_path;
82702d67d10Sthorpej 	char **paths;
82802d67d10Sthorpej 	void *pathsbuf = NULL;
82902d67d10Sthorpej 	FTS *fts;
83002d67d10Sthorpej 	FTSENT *chp, *p;
83102d67d10Sthorpej 	struct stat sb;
83202d67d10Sthorpej 
83302d67d10Sthorpej 	paths = evb_uboot_pkg_paths(params, NULL, &pathsbuf);
83402d67d10Sthorpej 	if (paths == NULL) {
83502d67d10Sthorpej 		warnx("No u-boot search path?");
83602d67d10Sthorpej 		return;
83702d67d10Sthorpej 	}
83802d67d10Sthorpej 
83902d67d10Sthorpej 	fts = fts_open(paths, FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOCHDIR, NULL);
84002d67d10Sthorpej 	if (fts == NULL ||
84102d67d10Sthorpej 	    (chp = fts_children(fts, 0)) == NULL) {
84202d67d10Sthorpej 		warn("Unable to search u-boot path");
84302d67d10Sthorpej 		if (fts != NULL)
84402d67d10Sthorpej 			fts_close(fts);
84502d67d10Sthorpej 		return;
84602d67d10Sthorpej 	}
84702d67d10Sthorpej 
84802d67d10Sthorpej 	chp = fts_children(fts, 0);
84902d67d10Sthorpej 
85002d67d10Sthorpej 	while ((p = fts_read(fts)) != NULL) {
85102d67d10Sthorpej 		if (p->fts_info != FTS_D)
85202d67d10Sthorpej 			continue;
85302d67d10Sthorpej 		overlay_path = make_path(overlay_pathbuf,
85402d67d10Sthorpej 		    sizeof(overlay_pathbuf), "%s/installboot.plist",
85502d67d10Sthorpej 		    p->fts_path);
85602d67d10Sthorpej 		if (overlay_path == NULL)
85702d67d10Sthorpej 			continue;
85802d67d10Sthorpej 		if (stat(overlay_path, &sb) < 0)
85902d67d10Sthorpej 			continue;
86002d67d10Sthorpej 		evb_db_load_overlay(params, overlay_path, p->fts_path);
86102d67d10Sthorpej 	}
86202d67d10Sthorpej 
86302d67d10Sthorpej 	fts_close(fts);
86402d67d10Sthorpej 
86502d67d10Sthorpej 	/*
866ba70c588Sandvar 	 * If the user specified a stage1 loader, then consult it last
86702d67d10Sthorpej 	 * for a possible u-boot package location.
86802d67d10Sthorpej 	 */
86902d67d10Sthorpej 	if (params->stage1 != NULL) {
87002d67d10Sthorpej 		overlay_path = make_path(overlay_pathbuf,
87102d67d10Sthorpej 		    sizeof(overlay_pathbuf), "%s/installboot.plist",
87202d67d10Sthorpej 		    params->stage1);
87302d67d10Sthorpej 		if (overlay_path != NULL) {
87402d67d10Sthorpej 			if (stat(overlay_path, &sb) == 0) {
87502d67d10Sthorpej 				evb_db_load_overlay(params, overlay_path,
87602d67d10Sthorpej 				    params->stage1);
87702d67d10Sthorpej 			}
87802d67d10Sthorpej 		}
87902d67d10Sthorpej 	}
88002d67d10Sthorpej }
88102d67d10Sthorpej 
88202d67d10Sthorpej /*
88302d67d10Sthorpej  * evb_db_load_base --
88402d67d10Sthorpej  *	Load the base board db.
88502d67d10Sthorpej  */
88602d67d10Sthorpej static bool
evb_db_load_base(ib_params * params)88702d67d10Sthorpej evb_db_load_base(ib_params *params)
88802d67d10Sthorpej {
88902d67d10Sthorpej 	char buf[PATH_MAX+1];
89002d67d10Sthorpej 	const char *path;
89102d67d10Sthorpej 	prop_dictionary_t board_db;
89202d67d10Sthorpej 	struct stat sb;
89302d67d10Sthorpej 
89402d67d10Sthorpej 	path = evb_db_base_path(params, buf, sizeof(buf));
89502d67d10Sthorpej 	if (path == NULL)
89602d67d10Sthorpej 		return false;
89702d67d10Sthorpej 
89802d67d10Sthorpej 	if (params->flags & IB_VERBOSE)
89902d67d10Sthorpej 		printf("Loading '%s'.\n", path);
90002d67d10Sthorpej 
90102d67d10Sthorpej 	if (stat(path, &sb) < 0) {
90202d67d10Sthorpej 		if (errno != ENOENT) {
90302d67d10Sthorpej 			warn("'%s'", path);
90402d67d10Sthorpej 			return false;
90502d67d10Sthorpej 		}
90602d67d10Sthorpej 		board_db = prop_dictionary_create();
90702d67d10Sthorpej 		assert(board_db != NULL);
90802d67d10Sthorpej 	} else {
90902d67d10Sthorpej 		board_db = prop_dictionary_internalize_from_file(path);
91002d67d10Sthorpej 		if (board_db == NULL) {
91102d67d10Sthorpej 			warnx("unable to parse board db '%s'", path);
91202d67d10Sthorpej 			return false;
91302d67d10Sthorpej 		}
91402d67d10Sthorpej 	}
91502d67d10Sthorpej 
91602d67d10Sthorpej 	if (prop_dictionary_count(board_db) == 0) {
91702d67d10Sthorpej 		/*
91802d67d10Sthorpej 		 * Oh well, maybe we'll load some overlays.
91902d67d10Sthorpej 		 */
92002d67d10Sthorpej 		goto done;
92102d67d10Sthorpej 	}
92202d67d10Sthorpej 
92302d67d10Sthorpej 	/*
92402d67d10Sthorpej 	 * Validate all of the board objects and remove any bad ones.
92502d67d10Sthorpej 	 */
92602d67d10Sthorpej 	prop_array_t all_board_keys = prop_dictionary_all_keys(board_db);
92702d67d10Sthorpej 	prop_object_iterator_t iter = prop_array_iterator(all_board_keys);
92802d67d10Sthorpej 	prop_dictionary_keysym_t key;
92902d67d10Sthorpej 	prop_dictionary_t board;
93002d67d10Sthorpej 	while ((key = prop_object_iterator_next(iter)) != NULL) {
93102d67d10Sthorpej 		board = prop_dictionary_get_keysym(board_db, key);
93202d67d10Sthorpej 		assert(board != NULL);
93302d67d10Sthorpej 		if (!validate_board_object(board, false)) {
93402d67d10Sthorpej 			warnx("invalid board object in '%s': '%s'", path,
935bba3c683Sthorpej 			    prop_dictionary_keysym_value(key));
93602d67d10Sthorpej 			prop_dictionary_remove_keysym(board_db, key);
93702d67d10Sthorpej 		}
93802d67d10Sthorpej 	}
93902d67d10Sthorpej 	prop_object_iterator_release(iter);
94002d67d10Sthorpej 	prop_object_release(all_board_keys);
94102d67d10Sthorpej 
94202d67d10Sthorpej  done:
94302d67d10Sthorpej 	params->mach_data = board_db;
94402d67d10Sthorpej 	return true;
94502d67d10Sthorpej }
94602d67d10Sthorpej 
94702d67d10Sthorpej /*
94802d67d10Sthorpej  * evb_db_load --
94902d67d10Sthorpej  *	Load the board database.
95002d67d10Sthorpej  */
95102d67d10Sthorpej bool
evb_db_load(ib_params * params)95202d67d10Sthorpej evb_db_load(ib_params *params)
95302d67d10Sthorpej {
95402d67d10Sthorpej 	if (!evb_db_load_base(params))
95502d67d10Sthorpej 		return false;
95602d67d10Sthorpej 	evb_db_load_overlays(params);
95702d67d10Sthorpej 
95802d67d10Sthorpej 	return true;
95902d67d10Sthorpej }
96002d67d10Sthorpej 
96102d67d10Sthorpej #if !HAVE_NBTOOL_CONFIG_H
96202d67d10Sthorpej /*
96302d67d10Sthorpej  * Native board name guessing methods.
96402d67d10Sthorpej  */
96502d67d10Sthorpej 
96602d67d10Sthorpej #ifdef SUPPORT_OPENFIRMWARE
96702d67d10Sthorpej static int
ofw_fd(void)96802d67d10Sthorpej ofw_fd(void)
96902d67d10Sthorpej {
97002d67d10Sthorpej 	static const char openfirm_path[] = "/dev/openfirm";
97102d67d10Sthorpej 
97202d67d10Sthorpej 	return open(openfirm_path, O_RDONLY);
97302d67d10Sthorpej }
97402d67d10Sthorpej 
97502d67d10Sthorpej static int
OF_finddevice(const char * name)97602d67d10Sthorpej OF_finddevice(const char *name)
97702d67d10Sthorpej {
97802d67d10Sthorpej 	struct ofiocdesc ofio = {
97902d67d10Sthorpej 		.of_name = __UNCONST(name),
98002d67d10Sthorpej 		.of_namelen = strlen(name),
98102d67d10Sthorpej 	};
98202d67d10Sthorpej 	int fd = ofw_fd();
98302d67d10Sthorpej 
98402d67d10Sthorpej 	if (fd == -1)
98502d67d10Sthorpej 		return -1;
98602d67d10Sthorpej 
98702d67d10Sthorpej 	if (ioctl(fd, OFIOCFINDDEVICE, &ofio) < 0) {
98802d67d10Sthorpej 		if (errno != ENOENT)
98902d67d10Sthorpej 			warn("OFIOCFINDDEVICE('%s')", name);
99002d67d10Sthorpej 		ofio.of_nodeid = -1;
99102d67d10Sthorpej 	}
99202d67d10Sthorpej 	(void) close(fd);
99302d67d10Sthorpej 
99402d67d10Sthorpej 	return ofio.of_nodeid;
99502d67d10Sthorpej }
99602d67d10Sthorpej 
99702d67d10Sthorpej static int
OF_getprop(int phandle,const char * prop,void * buf,size_t buflen)99802d67d10Sthorpej OF_getprop(int phandle, const char *prop, void *buf, size_t buflen)
99902d67d10Sthorpej {
100002d67d10Sthorpej 	struct ofiocdesc ofio = {
100102d67d10Sthorpej 		.of_nodeid = phandle,
100202d67d10Sthorpej 		.of_name = __UNCONST(prop),
100302d67d10Sthorpej 		.of_namelen = strlen(prop),
100402d67d10Sthorpej 		.of_buf = buf,
100502d67d10Sthorpej 		.of_buflen = buflen,
100602d67d10Sthorpej 	};
100702d67d10Sthorpej 	int fd = ofw_fd();
100802d67d10Sthorpej 
100902d67d10Sthorpej 	if (fd == -1)
101002d67d10Sthorpej 		return -1;
101102d67d10Sthorpej 
101202d67d10Sthorpej 	int save_errno = 0;
101302d67d10Sthorpej 
101402d67d10Sthorpej 	if (ioctl(fd, OFIOCGET, &ofio) < 0) {
101502d67d10Sthorpej 		save_errno = errno;
101602d67d10Sthorpej 		if (errno != ENOMEM && errno != ENOENT) {
101702d67d10Sthorpej 			save_errno = errno;
101802d67d10Sthorpej 			warn("OFIOCGET('%s')", prop);
101902d67d10Sthorpej 		}
102002d67d10Sthorpej 		ofio.of_buflen = -1;
102102d67d10Sthorpej 	}
102202d67d10Sthorpej 	(void) close(fd);
102302d67d10Sthorpej 	errno = save_errno;
102402d67d10Sthorpej 
102502d67d10Sthorpej 	return ofio.of_buflen;
102602d67d10Sthorpej }
102702d67d10Sthorpej 
102802d67d10Sthorpej static void *
ofw_getprop(int phandle,const char * prop,int * lenp)102902d67d10Sthorpej ofw_getprop(int phandle, const char *prop, int *lenp)
103002d67d10Sthorpej {
103102d67d10Sthorpej 	size_t buflen = 32;
103202d67d10Sthorpej 	void *buf = NULL;
103302d67d10Sthorpej 	int len;
103402d67d10Sthorpej 
103502d67d10Sthorpej 	for (;;) {
103602d67d10Sthorpej 		void *newbuf = realloc(buf, buflen);
103702d67d10Sthorpej 		if (newbuf == NULL) {
103802d67d10Sthorpej 			free(buf);
103902d67d10Sthorpej 			return NULL;
104002d67d10Sthorpej 		}
104102d67d10Sthorpej 		buf = newbuf;
104202d67d10Sthorpej 		switch (len = OF_getprop(phandle, prop, buf, buflen)) {
104302d67d10Sthorpej 		case -1:
104402d67d10Sthorpej 			if (errno != ENOMEM) {
104502d67d10Sthorpej 				free(buf);
104602d67d10Sthorpej 				return NULL;
104702d67d10Sthorpej 			}
104802d67d10Sthorpej 			buflen *= 2;
104902d67d10Sthorpej 			break;
105002d67d10Sthorpej 
105102d67d10Sthorpej 		default:
105202d67d10Sthorpej 			if (lenp)
105302d67d10Sthorpej 				*lenp = len;
105402d67d10Sthorpej 			return buf;
105502d67d10Sthorpej 		}
105602d67d10Sthorpej 	}
105702d67d10Sthorpej }
105802d67d10Sthorpej 
105902d67d10Sthorpej static evb_board
evb_db_get_board_from_ofw(ib_params * params,const char ** board_namep)106002d67d10Sthorpej evb_db_get_board_from_ofw(ib_params *params, const char **board_namep)
106102d67d10Sthorpej {
106202d67d10Sthorpej 	int phandle;
106302d67d10Sthorpej 	int compatible_len = 0;
106402d67d10Sthorpej 	char *compatible_buf;
106502d67d10Sthorpej 	const char *sp, *nsp;
106602d67d10Sthorpej 	evb_board board;
106702d67d10Sthorpej 
106802d67d10Sthorpej 	phandle = OF_finddevice("/");
106902d67d10Sthorpej 	if (phandle == -1) {
107002d67d10Sthorpej 		/* No OpenFirmware available. */
107102d67d10Sthorpej 		return NULL;
107202d67d10Sthorpej 	}
107302d67d10Sthorpej 
107402d67d10Sthorpej 	compatible_buf = ofw_getprop(phandle, "compatible", &compatible_len);
107502d67d10Sthorpej 
107602d67d10Sthorpej 	/*
107702d67d10Sthorpej 	 * We just leak compatible_buf on success.  Not a big deal since
107802d67d10Sthorpej 	 * we are not a long-running process.
107902d67d10Sthorpej 	 */
108002d67d10Sthorpej 
108102d67d10Sthorpej 	sp = compatible_buf;
108202d67d10Sthorpej 	while (compatible_len &&
108302d67d10Sthorpej 	       (nsp = memchr(sp, 0, compatible_len)) != NULL) {
108402d67d10Sthorpej 		if (params->flags & IB_VERBOSE)
108502d67d10Sthorpej 			printf("Checking OFW compatible string '%s'.\n", sp);
108602d67d10Sthorpej 		board = prop_dictionary_get(params->mach_data, sp);
108702d67d10Sthorpej 		if (board != NULL) {
108802d67d10Sthorpej 			if (board_namep)
108902d67d10Sthorpej 				*board_namep = sp;
109002d67d10Sthorpej 			return board;
109102d67d10Sthorpej 		}
109202d67d10Sthorpej 		nsp++;	/* skip over NUL */
109302d67d10Sthorpej 		compatible_len -= (nsp - sp);
109402d67d10Sthorpej 		sp = nsp;
109502d67d10Sthorpej 	}
109602d67d10Sthorpej 
109702d67d10Sthorpej 	free(compatible_buf);
109802d67d10Sthorpej 	return NULL;
109902d67d10Sthorpej }
110002d67d10Sthorpej #endif /* SUPPORT_OPENFIRMWARE */
110102d67d10Sthorpej 
110202d67d10Sthorpej #endif /* ! HAVE_NBTOOL_CONFIG_H */
110302d67d10Sthorpej 
110402d67d10Sthorpej /*
110502d67d10Sthorpej  * Host-tool and native board name guessing methods.
110602d67d10Sthorpej  */
110702d67d10Sthorpej 
110802d67d10Sthorpej #ifdef SUPPORT_FDT
110902d67d10Sthorpej static void *
load_dtb(ib_params * params)111002d67d10Sthorpej load_dtb(ib_params *params)
111102d67d10Sthorpej {
111202d67d10Sthorpej 	struct stat sb;
111302d67d10Sthorpej 	void *buf;
111402d67d10Sthorpej 	int fd;
111502d67d10Sthorpej 
111602d67d10Sthorpej 	if (stat(params->dtb, &sb) < 0) {
111702d67d10Sthorpej 		warn("%s", params->dtb);
111802d67d10Sthorpej 		return NULL;
111902d67d10Sthorpej 	}
112002d67d10Sthorpej 
112102d67d10Sthorpej 	buf = malloc((size_t)sb.st_size);
112202d67d10Sthorpej 	assert(buf != NULL);
112302d67d10Sthorpej 
112402d67d10Sthorpej 	if ((fd = open(params->dtb, O_RDONLY)) < 0) {
112502d67d10Sthorpej 		warn("%s", params->dtb);
112602d67d10Sthorpej 		free(buf);
112702d67d10Sthorpej 		return NULL;
112802d67d10Sthorpej 	}
112902d67d10Sthorpej 
113002d67d10Sthorpej 	if (read(fd, buf, (size_t)sb.st_size) != (ssize_t)sb.st_size) {
113102d67d10Sthorpej 		warn("read '%s'", params->dtb);
113202d67d10Sthorpej 		free(buf);
113302d67d10Sthorpej 		buf = NULL;
113402d67d10Sthorpej 	}
113502d67d10Sthorpej 	(void) close(fd);
113602d67d10Sthorpej 
113702d67d10Sthorpej 	return buf;
113802d67d10Sthorpej }
113902d67d10Sthorpej 
114002d67d10Sthorpej static evb_board
evb_db_get_board_from_dtb(ib_params * params,const char ** board_namep)114102d67d10Sthorpej evb_db_get_board_from_dtb(ib_params *params, const char **board_namep)
114202d67d10Sthorpej {
114302d67d10Sthorpej 	evb_board board = NULL;
114402d67d10Sthorpej 	void *fdt = NULL;
114502d67d10Sthorpej 	int error;
114602d67d10Sthorpej 
114702d67d10Sthorpej 	fdt = load_dtb(params);
114802d67d10Sthorpej 	if (fdt == NULL)
114902d67d10Sthorpej 		return NULL;
115002d67d10Sthorpej 
115102d67d10Sthorpej 	error = fdt_check_header(fdt);
115202d67d10Sthorpej 	if (error) {
115302d67d10Sthorpej 		warnx("%s: %s", params->dtb, fdt_strerror(error));
115402d67d10Sthorpej 		goto bad;
115502d67d10Sthorpej 	}
115602d67d10Sthorpej 
115702d67d10Sthorpej 	const int system_root = fdt_path_offset(fdt, "/");
115802d67d10Sthorpej 	if (system_root < 0) {
115902d67d10Sthorpej 		warnx("%s: unable to find node '/'", params->dtb);
116002d67d10Sthorpej 		goto bad;
116102d67d10Sthorpej 	}
116202d67d10Sthorpej 
116302d67d10Sthorpej 	const int system_ncompat = fdt_stringlist_count(fdt, system_root,
116402d67d10Sthorpej 	    "compatible");
116502d67d10Sthorpej 	if (system_ncompat <= 0) {
116602d67d10Sthorpej 		warnx("%s: no 'compatible' property on node '/'", params->dtb);
116702d67d10Sthorpej 		goto bad;
116802d67d10Sthorpej 	}
116902d67d10Sthorpej 
117002d67d10Sthorpej 	const char *compatible;
117102d67d10Sthorpej 	int si;
117202d67d10Sthorpej 	for (si = 0; si < system_ncompat; si++) {
117302d67d10Sthorpej 		compatible = fdt_stringlist_get(fdt, system_root,
117402d67d10Sthorpej 		    "compatible", si, NULL);
117502d67d10Sthorpej 		if (compatible == NULL)
117602d67d10Sthorpej 			continue;
117702d67d10Sthorpej 		if (params->flags & IB_VERBOSE)
117802d67d10Sthorpej 			printf("Checking FDT compatible string '%s'.\n",
117902d67d10Sthorpej 			    compatible);
118002d67d10Sthorpej 		board = prop_dictionary_get(params->mach_data, compatible);
118102d67d10Sthorpej 		if (board != NULL) {
118202d67d10Sthorpej 			/*
118302d67d10Sthorpej 			 * We just leak compatible on success.  Not a big
118402d67d10Sthorpej 			 * deal since we are not a long-running process.
118502d67d10Sthorpej 			 */
118602d67d10Sthorpej 			if (board_namep) {
118702d67d10Sthorpej 				*board_namep = strdup(compatible);
118802d67d10Sthorpej 				assert(*board_namep != NULL);
118902d67d10Sthorpej 			}
119002d67d10Sthorpej 			free(fdt);
119102d67d10Sthorpej 			return board;
119202d67d10Sthorpej 		}
119302d67d10Sthorpej 	}
119402d67d10Sthorpej 
119502d67d10Sthorpej  bad:
119602d67d10Sthorpej 	if (fdt != NULL)
119702d67d10Sthorpej 		free(fdt);
119802d67d10Sthorpej 	return NULL;
119902d67d10Sthorpej }
120002d67d10Sthorpej #endif /* SUPPORT_FDT */
120102d67d10Sthorpej 
120202d67d10Sthorpej /*
120302d67d10Sthorpej  * evb_db_get_board --
120402d67d10Sthorpej  *	Return the specified board object from the database.
120502d67d10Sthorpej  */
120602d67d10Sthorpej evb_board
evb_db_get_board(ib_params * params)120702d67d10Sthorpej evb_db_get_board(ib_params *params)
120802d67d10Sthorpej {
120902d67d10Sthorpej 	const char *board_name = NULL;
121002d67d10Sthorpej 	evb_board board = NULL;
121102d67d10Sthorpej 
121202d67d10Sthorpej #if !HAVE_NBTOOL_CONFIG_H
121302d67d10Sthorpej 	/*
121402d67d10Sthorpej 	 * If we're not a host tool, determine if we're running "natively".
121502d67d10Sthorpej 	 */
121602d67d10Sthorpej 	bool is_native = false;
121702d67d10Sthorpej 	struct utsname utsname;
121802d67d10Sthorpej 
121902d67d10Sthorpej 	if (uname(&utsname) < 0) {
122002d67d10Sthorpej 		warn("uname");
122102d67d10Sthorpej 	} else if (strcmp(utsname.machine, params->machine->name) == 0) {
122202d67d10Sthorpej 		is_native = true;
122302d67d10Sthorpej 	}
122402d67d10Sthorpej #endif /* ! HAVE_NBTOOL_CONFIG_H */
122502d67d10Sthorpej 
122602d67d10Sthorpej 	/*
122702d67d10Sthorpej 	 * Logic for determing board type that can be shared by host-tool
122802d67d10Sthorpej 	 * and native builds goes here.
122902d67d10Sthorpej 	 */
123002d67d10Sthorpej 
123102d67d10Sthorpej 	/*
123202d67d10Sthorpej 	 * Command-line argument trumps all.
123302d67d10Sthorpej 	 */
123402d67d10Sthorpej 	if (params->flags & IB_BOARD) {
123502d67d10Sthorpej 		board_name = params->board;
123602d67d10Sthorpej 	}
123702d67d10Sthorpej 
123802d67d10Sthorpej #ifdef SUPPORT_FDT
123902d67d10Sthorpej 	if (board_name == NULL && (params->flags & IB_DTB)) {
124002d67d10Sthorpej 		board = evb_db_get_board_from_dtb(params, &board_name);
124102d67d10Sthorpej 		if ((params->flags & IB_VERBOSE) && board != NULL)
124202d67d10Sthorpej 			printf("Found board '%s' from DTB data.\n", board_name);
124302d67d10Sthorpej #if !HAVE_NBTOOL_CONFIG_H
124402d67d10Sthorpej 		/*
124502d67d10Sthorpej 		 * If the user specified a DTB, then regardless of the
124602d67d10Sthorpej 		 * outcome, this is like specifying the board directly,
124702d67d10Sthorpej 		 * so native checks should be skipped.
124802d67d10Sthorpej 		 */
124902d67d10Sthorpej 		is_native = false;
125002d67d10Sthorpej #endif /* ! HAVE_NBTOOL_CONFIG_H */
125102d67d10Sthorpej 	}
125202d67d10Sthorpej #endif /* SUPPORT_FDT */
125302d67d10Sthorpej 
125402d67d10Sthorpej #if !HAVE_NBTOOL_CONFIG_H
125502d67d10Sthorpej 	/*
125602d67d10Sthorpej 	 * Non-host-tool logic for determining the board type goes here.
125702d67d10Sthorpej 	 */
125802d67d10Sthorpej 
125902d67d10Sthorpej #ifdef SUPPORT_OPENFIRMWARE
126002d67d10Sthorpej 	if (board_name == NULL && is_native) {
126102d67d10Sthorpej 		board = evb_db_get_board_from_ofw(params, &board_name);
126202d67d10Sthorpej 		if ((params->flags & IB_VERBOSE) && board != NULL)
126302d67d10Sthorpej 			printf("Found board '%s' from OFW data.\n", board_name);
126402d67d10Sthorpej 	}
126502d67d10Sthorpej #endif /* SUPPORT_OPENFIRMWARE */
126602d67d10Sthorpej 
126702d67d10Sthorpej 	/* Ensure is_native is consumed. */
126802d67d10Sthorpej 	if (is_native == false)
126902d67d10Sthorpej 		is_native = false;
127002d67d10Sthorpej 
127102d67d10Sthorpej #endif /* ! HAVE_NBTOOL_CONFIG_H */
127202d67d10Sthorpej 
127302d67d10Sthorpej 	/*
127402d67d10Sthorpej 	 * If all else fails, we can always rely on the user, right?
127502d67d10Sthorpej 	 */
127602d67d10Sthorpej 	if (board_name == NULL) {
127702d67d10Sthorpej 		if (!(params->flags & IB_BOARD)) {
127802d67d10Sthorpej 			warnx("Must specify board=...");
127902d67d10Sthorpej 			return NULL;
128002d67d10Sthorpej 		}
128102d67d10Sthorpej 		board_name = params->board;
128202d67d10Sthorpej 	}
128302d67d10Sthorpej 
128402d67d10Sthorpej 	assert(board_name != NULL);
128502d67d10Sthorpej 
128602d67d10Sthorpej 	if (board == NULL)
128702d67d10Sthorpej 		board = prop_dictionary_get(params->mach_data, board_name);
128802d67d10Sthorpej 	if (board == NULL)
128902d67d10Sthorpej 		warnx("Unknown board '%s'", board_name);
129002d67d10Sthorpej 
129102d67d10Sthorpej 	/* Ensure params->board is always valid. */
129202d67d10Sthorpej 	params->board = board_name;
129302d67d10Sthorpej 
129402d67d10Sthorpej 	if (params->flags & IB_VERBOSE) {
129502d67d10Sthorpej 		printf("Board: %s\n", evb_board_get_description(params, board));
129602d67d10Sthorpej 	}
129702d67d10Sthorpej 
129802d67d10Sthorpej 	return board;
129902d67d10Sthorpej }
130002d67d10Sthorpej 
130102d67d10Sthorpej /*
130202d67d10Sthorpej  * evb_db_list_boards --
130302d67d10Sthorpej  *	Print the list of known boards to the specified output stream.
130402d67d10Sthorpej  */
130502d67d10Sthorpej void
evb_db_list_boards(ib_params * params,FILE * out)130602d67d10Sthorpej evb_db_list_boards(ib_params *params, FILE *out)
130702d67d10Sthorpej {
130802d67d10Sthorpej 	prop_object_iterator_t iter;
130902d67d10Sthorpej 	prop_dictionary_keysym_t key;
131002d67d10Sthorpej 	evb_board board;
131102d67d10Sthorpej 	const char *uboot_pkg;
131202d67d10Sthorpej 	const char *uboot_path;
131302d67d10Sthorpej 
131402d67d10Sthorpej 	/*
131502d67d10Sthorpej 	 * By default, we only list boards that we have a u-boot
131602d67d10Sthorpej 	 * package installed for, or if we know which package you
131702d67d10Sthorpej 	 * need to install.  You get the full monty in verbose mode.
131802d67d10Sthorpej 	 */
131902d67d10Sthorpej 
132002d67d10Sthorpej 	iter = prop_dictionary_iterator(params->mach_data);
132102d67d10Sthorpej 	while ((key = prop_object_iterator_next(iter)) != NULL) {
132202d67d10Sthorpej 		board = prop_dictionary_get_keysym(params->mach_data, key);
132302d67d10Sthorpej 		assert(board != NULL);
132402d67d10Sthorpej 		uboot_pkg = evb_board_get_uboot_pkg(params, board);
132502d67d10Sthorpej 		uboot_path = evb_board_get_uboot_path(params, board);
132602d67d10Sthorpej 
132702d67d10Sthorpej 		if (uboot_pkg == NULL && uboot_path == NULL &&
132802d67d10Sthorpej 		    !(params->flags & IB_VERBOSE))
132902d67d10Sthorpej 			continue;
133002d67d10Sthorpej 
133102d67d10Sthorpej 		fprintf(out, "%-30s %s\n",
1332bba3c683Sthorpej 		    prop_dictionary_keysym_value(key),
133302d67d10Sthorpej 		    evb_board_get_description(params, board));
133402d67d10Sthorpej 
133502d67d10Sthorpej 		if ((params->flags & IB_VERBOSE) && uboot_path) {
133602d67d10Sthorpej 			fprintf(out, "\t(u-boot package found at %s)\n",
133702d67d10Sthorpej 			    uboot_path);
133802d67d10Sthorpej 		} else if ((params->flags & IB_VERBOSE) && uboot_pkg) {
133902d67d10Sthorpej 			fprintf(out,
134002d67d10Sthorpej 			    "\t(install the sysutils/u-boot-%s package)\n",
134102d67d10Sthorpej 			    uboot_pkg);
134202d67d10Sthorpej 		}
134302d67d10Sthorpej 	}
134402d67d10Sthorpej 	prop_object_iterator_release(iter);
134502d67d10Sthorpej }
134602d67d10Sthorpej 
134702d67d10Sthorpej /*
134802d67d10Sthorpej  * evb_board_get_description --
134902d67d10Sthorpej  *	Return the description for the specified board.
135002d67d10Sthorpej  */
135102d67d10Sthorpej const char *
evb_board_get_description(ib_params * params,evb_board board)135202d67d10Sthorpej evb_board_get_description(ib_params *params, evb_board board)
135302d67d10Sthorpej {
135402d67d10Sthorpej 	prop_string_t string;
135502d67d10Sthorpej 
135602d67d10Sthorpej 	string = prop_dictionary_get(board, board_description_key);
1357bba3c683Sthorpej 	return prop_string_value(string);
135802d67d10Sthorpej }
135902d67d10Sthorpej 
136002d67d10Sthorpej /*
136102d67d10Sthorpej  * evb_board_get_uboot_pkg --
136202d67d10Sthorpej  *	Return the u-boot package name for the specified board.
136302d67d10Sthorpej  */
136402d67d10Sthorpej const char *
evb_board_get_uboot_pkg(ib_params * params,evb_board board)136502d67d10Sthorpej evb_board_get_uboot_pkg(ib_params *params, evb_board board)
136602d67d10Sthorpej {
136702d67d10Sthorpej 	prop_string_t string;
136802d67d10Sthorpej 
136902d67d10Sthorpej 	string = prop_dictionary_get(board, board_u_boot_pkg_key);
137002d67d10Sthorpej 	if (string == NULL)
137102d67d10Sthorpej 		return NULL;
1372bba3c683Sthorpej 	return prop_string_value(string);
137302d67d10Sthorpej }
137402d67d10Sthorpej 
137502d67d10Sthorpej /*
137602d67d10Sthorpej  * evb_board_get_uboot_path --
137702d67d10Sthorpej  *	Return the u-boot installed package path for the specified board.
137802d67d10Sthorpej  */
137902d67d10Sthorpej const char *
evb_board_get_uboot_path(ib_params * params,evb_board board)138002d67d10Sthorpej evb_board_get_uboot_path(ib_params *params, evb_board board)
138102d67d10Sthorpej {
138202d67d10Sthorpej 	prop_string_t string;
138302d67d10Sthorpej 
138402d67d10Sthorpej 	string = prop_dictionary_get(board, board_u_boot_path_key);
138502d67d10Sthorpej 	if (string == NULL)
138602d67d10Sthorpej 		return NULL;
1387bba3c683Sthorpej 	return prop_string_value(string);
138802d67d10Sthorpej }
138902d67d10Sthorpej 
139002d67d10Sthorpej /*
139102d67d10Sthorpej  * evb_board_get_uboot_install --
139202d67d10Sthorpej  *	Return the u-boot install object for the specified board,
139302d67d10Sthorpej  *	corresponding to the media specified by the user.
139402d67d10Sthorpej  */
139502d67d10Sthorpej evb_ubinstall
evb_board_get_uboot_install(ib_params * params,evb_board board)139602d67d10Sthorpej evb_board_get_uboot_install(ib_params *params, evb_board board)
139702d67d10Sthorpej {
139802d67d10Sthorpej 	evb_ubinstall install;
139902d67d10Sthorpej 
140002d67d10Sthorpej 	install = prop_dictionary_get(board, board_u_boot_install_key);
140102d67d10Sthorpej 
140202d67d10Sthorpej 	if (!(params->flags & IB_MEDIA)) {
140302d67d10Sthorpej 		if (install == NULL) {
140402d67d10Sthorpej 			warnx("Must specify media=... for board '%s'",
140502d67d10Sthorpej 			    params->board);
140602d67d10Sthorpej 			goto list_media;
140702d67d10Sthorpej 		}
140802d67d10Sthorpej 		return install;
140902d67d10Sthorpej 	}
141002d67d10Sthorpej 
141102d67d10Sthorpej 	/* media=... was specified by the user. */
141202d67d10Sthorpej 
141302d67d10Sthorpej 	if (install) {
141402d67d10Sthorpej 		warnx("media=... is not a valid option for board '%s'",
141502d67d10Sthorpej 		    params->board);
141602d67d10Sthorpej 		return NULL;
141702d67d10Sthorpej 	}
141802d67d10Sthorpej 
141902d67d10Sthorpej 	char install_key[128];
142002d67d10Sthorpej 	int n = snprintf(install_key, sizeof(install_key), "%s-%s",
142102d67d10Sthorpej 	    board_u_boot_install_key, params->media);
142202d67d10Sthorpej 	if (n < 0 || (size_t)n >= sizeof(install_key))
14238012ca3fSmsaitoh 		goto invalid_media;
142402d67d10Sthorpej 	install = prop_dictionary_get(board, install_key);
14256e45676fSthorpej 	if (install != NULL) {
14266e45676fSthorpej 		if (prop_object_type(install) == PROP_TYPE_STRING) {
14276e45676fSthorpej 			/*
14286e45676fSthorpej 			 * This is an alias.  Fetch the target.  We
14296e45676fSthorpej 			 * have already validated that the target
14306e45676fSthorpej 			 * exists.
14316e45676fSthorpej 			 */
14326e45676fSthorpej 			install = prop_dictionary_get(board,
1433bba3c683Sthorpej 			    prop_string_value((prop_string_t)install));
14346e45676fSthorpej 		}
143502d67d10Sthorpej 		return install;
14366e45676fSthorpej 	}
143702d67d10Sthorpej  invalid_media:
143802d67d10Sthorpej 	warnx("invalid media specification: '%s'", params->media);
143902d67d10Sthorpej  list_media:
144002d67d10Sthorpej 	fprintf(stderr, "Valid media types:");
144102d67d10Sthorpej 	prop_array_t array = evb_board_copy_uboot_media(params, board);
144202d67d10Sthorpej 	assert(array != NULL);
144302d67d10Sthorpej 	prop_object_iterator_t iter = prop_array_iterator(array);
144402d67d10Sthorpej 	prop_string_t string;
144502d67d10Sthorpej 	while ((string = prop_object_iterator_next(iter)) != NULL)
1446bba3c683Sthorpej 		fprintf(stderr, " %s", prop_string_value(string));
144702d67d10Sthorpej 	fprintf(stderr, "\n");
144802d67d10Sthorpej 	prop_object_iterator_release(iter);
144902d67d10Sthorpej 	prop_object_release(array);
145002d67d10Sthorpej 
145102d67d10Sthorpej 	return NULL;
145202d67d10Sthorpej }
145302d67d10Sthorpej 
145402d67d10Sthorpej /*
145502d67d10Sthorpej  * evb_board_copy_uboot_media --
145602d67d10Sthorpej  *	Return the valid media types for the given board as an array
145702d67d10Sthorpej  *	of strings.
145802d67d10Sthorpej  *
145902d67d10Sthorpej  *	Follows the create rule; caller is responsible for releasing
146002d67d10Sthorpej  *	the array.
146102d67d10Sthorpej  */
146202d67d10Sthorpej prop_array_t
evb_board_copy_uboot_media(ib_params * params,evb_board board)146302d67d10Sthorpej evb_board_copy_uboot_media(ib_params *params, evb_board board)
146402d67d10Sthorpej {
146502d67d10Sthorpej 	prop_array_t array = prop_array_create();
146602d67d10Sthorpej 	prop_object_iterator_t iter = prop_dictionary_iterator(board);
146702d67d10Sthorpej 	prop_string_t string;
146802d67d10Sthorpej 	prop_dictionary_keysym_t key;
146902d67d10Sthorpej 	const char *cp;
147002d67d10Sthorpej 
147102d67d10Sthorpej 	assert(array != NULL);
147202d67d10Sthorpej 	assert(iter != NULL);
147302d67d10Sthorpej 
147402d67d10Sthorpej 	while ((key = prop_object_iterator_next(iter)) != NULL) {
1475bba3c683Sthorpej 		cp = prop_dictionary_keysym_value(key);
147602d67d10Sthorpej 		if (strcmp(cp, board_u_boot_install_key) == 0 ||
147702d67d10Sthorpej 		    strncmp(cp, board_u_boot_install_key,
147802d67d10Sthorpej 			    sizeof(board_u_boot_install_key) - 1) != 0)
147902d67d10Sthorpej 			continue;
1480bba3c683Sthorpej 		string = prop_string_create_copy(strrchr(cp, '-')+1);
148102d67d10Sthorpej 		assert(string != NULL);
148202d67d10Sthorpej 		prop_array_add(array, string);
148302d67d10Sthorpej 		prop_object_release(string);
148402d67d10Sthorpej 	}
148502d67d10Sthorpej 	prop_object_iterator_release(iter);
148602d67d10Sthorpej 	return array;
148702d67d10Sthorpej }
148802d67d10Sthorpej 
148902d67d10Sthorpej /*
149002d67d10Sthorpej  * evb_ubinstall_get_steps --
149102d67d10Sthorpej  *	Get the install steps for a given install object.
149202d67d10Sthorpej  */
149302d67d10Sthorpej evb_ubsteps
evb_ubinstall_get_steps(ib_params * params,evb_ubinstall install)149402d67d10Sthorpej evb_ubinstall_get_steps(ib_params *params, evb_ubinstall install)
149502d67d10Sthorpej {
149602d67d10Sthorpej 	return prop_array_iterator(install);
149702d67d10Sthorpej }
149802d67d10Sthorpej 
149902d67d10Sthorpej /*
150002d67d10Sthorpej  * evb_ubsteps_next_step --
150102d67d10Sthorpej  *	Return the next step in the install object.
150202d67d10Sthorpej  *
150302d67d10Sthorpej  *	N.B. The iterator is released upon termination.
150402d67d10Sthorpej  */
150502d67d10Sthorpej evb_ubstep
evb_ubsteps_next_step(ib_params * params,evb_ubsteps steps)150602d67d10Sthorpej evb_ubsteps_next_step(ib_params *params, evb_ubsteps steps)
150702d67d10Sthorpej {
150802d67d10Sthorpej 	prop_dictionary_t step = prop_object_iterator_next(steps);
150902d67d10Sthorpej 
151002d67d10Sthorpej 	/* If we are out of steps, release the iterator. */
151102d67d10Sthorpej 	if (step == NULL)
151202d67d10Sthorpej 		prop_object_iterator_release(steps);
151302d67d10Sthorpej 
151402d67d10Sthorpej 	return step;
151502d67d10Sthorpej }
151602d67d10Sthorpej 
151702d67d10Sthorpej /*
151802d67d10Sthorpej  * evb_ubstep_get_file_name --
151902d67d10Sthorpej  *	Returns the input file name for the step.
152002d67d10Sthorpej  */
152102d67d10Sthorpej const char *
evb_ubstep_get_file_name(ib_params * params,evb_ubstep step)152202d67d10Sthorpej evb_ubstep_get_file_name(ib_params *params, evb_ubstep step)
152302d67d10Sthorpej {
152402d67d10Sthorpej 	prop_string_t string = prop_dictionary_get(step, step_file_name_key);
1525bba3c683Sthorpej 	return prop_string_value(string);
152602d67d10Sthorpej }
152702d67d10Sthorpej 
152802d67d10Sthorpej /*
152902d67d10Sthorpej  * evb_ubstep_get_file_offset --
153002d67d10Sthorpej  *	Returns the input file offset for the step.
153102d67d10Sthorpej  */
153202d67d10Sthorpej uint64_t
evb_ubstep_get_file_offset(ib_params * params,evb_ubstep step)153302d67d10Sthorpej evb_ubstep_get_file_offset(ib_params *params, evb_ubstep step)
153402d67d10Sthorpej {
153502d67d10Sthorpej 	prop_number_t number = prop_dictionary_get(step, step_file_offset_key);
153602d67d10Sthorpej 	if (number != NULL)
1537bba3c683Sthorpej 		return prop_number_unsigned_value(number);
153802d67d10Sthorpej 	return 0;
153902d67d10Sthorpej }
154002d67d10Sthorpej 
154102d67d10Sthorpej /*
154202d67d10Sthorpej  * evb_ubstep_get_file_size --
154302d67d10Sthorpej  *	Returns the size of the input file to copy for this step, or
154402d67d10Sthorpej  *	zero if the remainder of the file should be copied.
154502d67d10Sthorpej  */
154602d67d10Sthorpej uint64_t
evb_ubstep_get_file_size(ib_params * params,evb_ubstep step)154702d67d10Sthorpej evb_ubstep_get_file_size(ib_params *params, evb_ubstep step)
154802d67d10Sthorpej {
154902d67d10Sthorpej 	prop_number_t number = prop_dictionary_get(step, step_file_size_key);
155002d67d10Sthorpej 	if (number != NULL)
1551bba3c683Sthorpej 		return prop_number_unsigned_value(number);
155202d67d10Sthorpej 	return 0;
155302d67d10Sthorpej }
155402d67d10Sthorpej 
155502d67d10Sthorpej /*
155602d67d10Sthorpej  * evb_ubstep_get_image_offset --
155702d67d10Sthorpej  *	Returns the offset into the destination image / device to
155802d67d10Sthorpej  *	copy the input file.
155902d67d10Sthorpej  */
156002d67d10Sthorpej uint64_t
evb_ubstep_get_image_offset(ib_params * params,evb_ubstep step)156102d67d10Sthorpej evb_ubstep_get_image_offset(ib_params *params, evb_ubstep step)
156202d67d10Sthorpej {
156302d67d10Sthorpej 	prop_number_t number = prop_dictionary_get(step, step_image_offset_key);
156402d67d10Sthorpej 	if (number != NULL)
1565bba3c683Sthorpej 		return prop_number_unsigned_value(number);
156602d67d10Sthorpej 	return 0;
156702d67d10Sthorpej }
156802d67d10Sthorpej 
156902d67d10Sthorpej /*
15706e45676fSthorpej  * evb_ubstep_get_input_block_size --
15716e45676fSthorpej  *	Returns the input block size to use when reading the boot loader
15726e45676fSthorpej  *	file.
15736e45676fSthorpej  */
15746e45676fSthorpej uint64_t
evb_ubstep_get_input_block_size(ib_params * params,evb_ubstep step)15756e45676fSthorpej evb_ubstep_get_input_block_size(ib_params *params, evb_ubstep step)
15766e45676fSthorpej {
15776e45676fSthorpej 	prop_number_t number = prop_dictionary_get(step,
15786e45676fSthorpej 						   step_input_block_size_key);
15796e45676fSthorpej 	if (number != NULL)
1580bba3c683Sthorpej 		return prop_number_unsigned_value(number);
15816e45676fSthorpej 	return 0;
15826e45676fSthorpej }
15836e45676fSthorpej 
15846e45676fSthorpej /*
15856e45676fSthorpej  * evb_ubstep_get_input_pad_size --
15866e45676fSthorpej  *	Returns the input pad size to use when reading the boot loader
15876e45676fSthorpej  *	file.
15886e45676fSthorpej  */
15896e45676fSthorpej uint64_t
evb_ubstep_get_input_pad_size(ib_params * params,evb_ubstep step)15906e45676fSthorpej evb_ubstep_get_input_pad_size(ib_params *params, evb_ubstep step)
15916e45676fSthorpej {
15926e45676fSthorpej 	prop_number_t number = prop_dictionary_get(step,
15936e45676fSthorpej 						   step_input_pad_size_key);
15946e45676fSthorpej 	if (number != NULL)
1595bba3c683Sthorpej 		return prop_number_unsigned_value(number);
15966e45676fSthorpej 	return 0;
15976e45676fSthorpej }
15986e45676fSthorpej 
15996e45676fSthorpej /*
16006e45676fSthorpej  * evb_ubstep_get_output_size --
16016e45676fSthorpej  *	Returns the total output size that will be written to the
16026e45676fSthorpej  *	output device.
16036e45676fSthorpej  */
16046e45676fSthorpej uint64_t
evb_ubstep_get_output_size(ib_params * params,evb_ubstep step)16056e45676fSthorpej evb_ubstep_get_output_size(ib_params *params, evb_ubstep step)
16066e45676fSthorpej {
16076e45676fSthorpej 	prop_number_t number = prop_dictionary_get(step, step_output_size_key);
16086e45676fSthorpej 	if (number != NULL)
1609bba3c683Sthorpej 		return prop_number_unsigned_value(number);
16106e45676fSthorpej 	return 0;
16116e45676fSthorpej }
16126e45676fSthorpej 
16136e45676fSthorpej /*
16146e45676fSthorpej  * evb_ubstep_get_output_block_size --
16156e45676fSthorpej  *	Returns the block size that must be written to the output device.
16166e45676fSthorpej  */
16176e45676fSthorpej uint64_t
evb_ubstep_get_output_block_size(ib_params * params,evb_ubstep step)16186e45676fSthorpej evb_ubstep_get_output_block_size(ib_params *params, evb_ubstep step)
16196e45676fSthorpej {
16206e45676fSthorpej 	prop_number_t number = prop_dictionary_get(step,
16216e45676fSthorpej 						   step_output_block_size_key);
16226e45676fSthorpej 	if (number != NULL)
1623bba3c683Sthorpej 		return prop_number_unsigned_value(number);
16246e45676fSthorpej 	return 0;
16256e45676fSthorpej }
16266e45676fSthorpej 
16276e45676fSthorpej /*
162802d67d10Sthorpej  * evb_ubstep_preserves_partial_block --
162902d67d10Sthorpej  *	Returns true if the step preserves a partial block.
163002d67d10Sthorpej  */
163102d67d10Sthorpej bool
evb_ubstep_preserves_partial_block(ib_params * params,evb_ubstep step)163202d67d10Sthorpej evb_ubstep_preserves_partial_block(ib_params *params, evb_ubstep step)
163302d67d10Sthorpej {
163402d67d10Sthorpej 	prop_bool_t val = prop_dictionary_get(step, step_preserve_key);
163502d67d10Sthorpej 	if (val != NULL)
163602d67d10Sthorpej 		return prop_bool_true(val);
163702d67d10Sthorpej 	return false;
163802d67d10Sthorpej }
163902d67d10Sthorpej 
164002d67d10Sthorpej /*
164102d67d10Sthorpej  * evb_uboot_file_path --
164202d67d10Sthorpej  *	Build a file path from the u-boot base path in the board object
164302d67d10Sthorpej  *	and the file name in the step object.
164402d67d10Sthorpej  */
164502d67d10Sthorpej static const char *
evb_uboot_file_path(ib_params * params,evb_board board,evb_ubstep step,char * buf,size_t bufsize)164602d67d10Sthorpej evb_uboot_file_path(ib_params *params, evb_board board, evb_ubstep step,
164702d67d10Sthorpej     char *buf, size_t bufsize)
164802d67d10Sthorpej {
164902d67d10Sthorpej 	const char *base_path = evb_board_get_uboot_path(params, board);
165002d67d10Sthorpej 	const char *file_name = evb_ubstep_get_file_name(params, step);
165102d67d10Sthorpej 
165202d67d10Sthorpej 	if (base_path == NULL || file_name == NULL)
165302d67d10Sthorpej 		return NULL;
165402d67d10Sthorpej 
165502d67d10Sthorpej 	return make_path(buf, bufsize, "%s/%s", base_path, file_name);
165602d67d10Sthorpej }
165702d67d10Sthorpej 
165802d67d10Sthorpej /*
165902d67d10Sthorpej  * evb_uboot_do_step --
166002d67d10Sthorpej  *	Given a evb_ubstep, do the deed.
166102d67d10Sthorpej  */
166202d67d10Sthorpej static int
evb_uboot_do_step(ib_params * params,const char * uboot_file,evb_ubstep step)166302d67d10Sthorpej evb_uboot_do_step(ib_params *params, const char *uboot_file, evb_ubstep step)
166402d67d10Sthorpej {
166502d67d10Sthorpej 	struct stat sb;
166602d67d10Sthorpej 	int ifd = -1;
16676e45676fSthorpej 	char *blockbuf = NULL;
166802d67d10Sthorpej 	off_t curoffset;
16696e45676fSthorpej 	off_t file_remaining;
167002d67d10Sthorpej 	bool rv = false;
167102d67d10Sthorpej 
167202d67d10Sthorpej 	uint64_t file_size = evb_ubstep_get_file_size(params, step);
167302d67d10Sthorpej 	uint64_t file_offset = evb_ubstep_get_file_offset(params, step);
167402d67d10Sthorpej 	uint64_t image_offset = evb_ubstep_get_image_offset(params, step);
16756e45676fSthorpej 	uint64_t output_size = evb_ubstep_get_output_size(params, step);
16766e45676fSthorpej 	size_t   output_block_size =
16776e45676fSthorpej 			(size_t)evb_ubstep_get_output_block_size(params, step);
16786e45676fSthorpej 	size_t   input_block_size =
16796e45676fSthorpej 			(size_t)evb_ubstep_get_input_block_size(params, step);
16806e45676fSthorpej 	size_t   input_pad_size =
16816e45676fSthorpej 			(size_t)evb_ubstep_get_input_pad_size(params, step);
16826e45676fSthorpej 	bool	 preserves_partial_block =
16836e45676fSthorpej 			evb_ubstep_preserves_partial_block(params, step);
16846e45676fSthorpej 	const char *uboot_file_name =
16856e45676fSthorpej 			evb_ubstep_get_file_name(params, step);
168602d67d10Sthorpej 
16876e45676fSthorpej 	if (input_block_size == 0 && output_block_size == 0) {
16886e45676fSthorpej 		if (params->flags & IB_VERBOSE) {
16896e45676fSthorpej 			printf("Defaulting input-block-size and "
16906e45676fSthorpej 			       "output-block-size to sectorsize "
16916e45676fSthorpej 			       "(%" PRIu32 ")\n", params->sectorsize);
16926e45676fSthorpej 		}
16936e45676fSthorpej 		input_block_size = output_block_size = params->sectorsize;
16946e45676fSthorpej 	} else if (input_block_size != 0 && output_block_size == 0) {
16956e45676fSthorpej 		if (params->flags & IB_VERBOSE) {
16966e45676fSthorpej 			printf("Defaulting output-block-size to "
16976e45676fSthorpej 			       "input-block-size (%zu)\n",
16986e45676fSthorpej 			       input_block_size);
16996e45676fSthorpej 		}
17006e45676fSthorpej 		output_block_size = input_block_size;
17016e45676fSthorpej 	} else if (output_block_size != 0 && input_block_size == 0) {
17026e45676fSthorpej 		if (params->flags & IB_VERBOSE) {
17036e45676fSthorpej 			printf("Defaulting input-block-size to "
17046e45676fSthorpej 			       "output-block-size (%zu)\n",
17056e45676fSthorpej 			       output_block_size);
17066e45676fSthorpej 		}
17076e45676fSthorpej 		input_block_size = output_block_size;
17086e45676fSthorpej 	}
17096e45676fSthorpej 
17106e45676fSthorpej 	if (output_block_size % params->sectorsize) {
17116e45676fSthorpej 		warnx("output-block-size (%zu) is not a multiple of "
17126e45676fSthorpej 		      "device sector size (%" PRIu32 ")",
17136e45676fSthorpej 		      output_block_size, params->sectorsize);
17146e45676fSthorpej 		goto out;
17156e45676fSthorpej 	}
17166e45676fSthorpej 
17176e45676fSthorpej 	if ((input_block_size + input_pad_size) > output_block_size) {
17186e45676fSthorpej 		warnx("input-{block+pad}-size (%zu) is larger than "
17196e45676fSthorpej 		      "output-block-size (%zu)",
17206e45676fSthorpej 		      input_block_size + input_pad_size,
17216e45676fSthorpej 		      output_block_size);
17226e45676fSthorpej 		goto out;
17236e45676fSthorpej 	}
17246e45676fSthorpej 
17256e45676fSthorpej 	if (output_block_size % (input_block_size + input_pad_size)) {
17266e45676fSthorpej 		warnx("output-block-size (%zu) it not a multiple of "
17276e45676fSthorpej 		      "input-{block+pad}-size (%zu)",
17286e45676fSthorpej 		      output_block_size,
17296e45676fSthorpej 		      input_block_size + input_pad_size);
17306e45676fSthorpej 		goto out;
17316e45676fSthorpej 	}
17326e45676fSthorpej 
17336e45676fSthorpej 	blockbuf = malloc(output_block_size);
173402d67d10Sthorpej 	if (blockbuf == NULL)
173502d67d10Sthorpej 		goto out;
173602d67d10Sthorpej 
173702d67d10Sthorpej 	ifd = open(uboot_file, O_RDONLY);
173802d67d10Sthorpej 	if (ifd < 0) {
173902d67d10Sthorpej 		warn("open '%s'", uboot_file);
174002d67d10Sthorpej 		goto out;
174102d67d10Sthorpej 	}
174202d67d10Sthorpej 	if (fstat(ifd, &sb) < 0) {
174302d67d10Sthorpej 		warn("fstat '%s'", uboot_file);
174402d67d10Sthorpej 		goto out;
174502d67d10Sthorpej 	}
174602d67d10Sthorpej 
174702d67d10Sthorpej 	if (file_size)
17486e45676fSthorpej 		file_remaining = (off_t)file_size;
174902d67d10Sthorpej 	else
17506e45676fSthorpej 		file_remaining = sb.st_size - (off_t)file_offset;
17516e45676fSthorpej 
17526e45676fSthorpej 	if (output_size == 0) {
17536e45676fSthorpej 		output_size = roundup(file_remaining, output_block_size);
17546e45676fSthorpej 	} else if ((uint64_t)file_remaining > output_size) {
17556e45676fSthorpej 		warnx("file size (%lld) is larger than output-size (%" PRIu64
17566e45676fSthorpej 		      ")", (long long)file_remaining, output_size);
17576e45676fSthorpej 		goto out;
17586e45676fSthorpej 	}
175902d67d10Sthorpej 
176002d67d10Sthorpej 	if (params->flags & IB_VERBOSE) {
176102d67d10Sthorpej 		if (file_offset) {
17626e45676fSthorpej 			printf("Writing '%s' %lld @ %" PRIu64
17636e45676fSthorpej 			       "to '%s' @  %" PRIu64 "\n",
17646e45676fSthorpej 			       uboot_file_name, (long long)file_remaining,
17656e45676fSthorpej 			       file_offset, params->filesystem, image_offset);
176602d67d10Sthorpej 		} else {
17676e45676fSthorpej 			printf("Writing '%s' %lld to '%s' @ %" PRIu64 "\n",
17686e45676fSthorpej 			       uboot_file_name, (long long)file_remaining,
17696e45676fSthorpej 			       params->filesystem, image_offset);
177002d67d10Sthorpej 		}
177102d67d10Sthorpej 	}
177202d67d10Sthorpej 
177302d67d10Sthorpej 	if (lseek(ifd, (off_t)file_offset, SEEK_SET) < 0) {
177402d67d10Sthorpej 		warn("lseek '%s' @ %" PRIu64, uboot_file,
177502d67d10Sthorpej 		    file_offset);
177602d67d10Sthorpej 		goto out;
177702d67d10Sthorpej 	}
177802d67d10Sthorpej 
17796e45676fSthorpej 	for (curoffset = (off_t)image_offset;
17806e45676fSthorpej 	     output_size != 0;
17816e45676fSthorpej 	     curoffset += output_block_size, output_size -= output_block_size) {
17826e45676fSthorpej 
17836e45676fSthorpej 		size_t outblock_remaining;
17846e45676fSthorpej 		size_t this_inblock;
17856e45676fSthorpej 		char *fill;
17866e45676fSthorpej 
17876e45676fSthorpej 		/*
17886e45676fSthorpej 		 * Initialize the output buffer.  We're either
17896e45676fSthorpej 		 * filling it with zeros, or we're preserving
17906e45676fSthorpej 		 * device contents that we don't overwrite.
17916e45676fSthorpej 		 */
17926e45676fSthorpej 		memset(blockbuf, 0, output_block_size);
17936e45676fSthorpej 		if (preserves_partial_block) {
179402d67d10Sthorpej 			if (params->flags & IB_VERBOSE) {
17956e45676fSthorpej 				printf("(Reading '%s' -- %zu @ %lld)\n",
179602d67d10Sthorpej 				       params->filesystem,
17976e45676fSthorpej 				       output_block_size,
179802d67d10Sthorpej 				       (long long)curoffset);
179902d67d10Sthorpej 			}
180002d67d10Sthorpej 			if (pread(params->fsfd, blockbuf,
18016e45676fSthorpej 				  output_block_size, curoffset) < 0) {
180202d67d10Sthorpej 				warn("pread '%s'", params->filesystem);
180302d67d10Sthorpej 				goto out;
180402d67d10Sthorpej 			}
180502d67d10Sthorpej 		}
18066e45676fSthorpej 
18076e45676fSthorpej 		/*
18086e45676fSthorpej 		 * Fill the output buffer with the file contents,
18096e45676fSthorpej 		 * interleaved with padding as necessary.  (If
18106e45676fSthorpej 		 * there is no file left, we're going to be left
18116e45676fSthorpej 		 * with padding to cover the output-size.)
18126e45676fSthorpej 		 */
18136e45676fSthorpej 		for (outblock_remaining = output_block_size, fill = blockbuf;
18146e45676fSthorpej 		     outblock_remaining != 0;
18156e45676fSthorpej 		     fill += input_block_size + input_pad_size,
18166e45676fSthorpej 		     outblock_remaining -= input_block_size + input_pad_size) {
18176e45676fSthorpej 
18186e45676fSthorpej 			this_inblock = input_block_size;
18196e45676fSthorpej 			if ((off_t)this_inblock > file_remaining) {
18206e45676fSthorpej 				this_inblock = file_remaining;
182102d67d10Sthorpej 			}
18226e45676fSthorpej 
18236e45676fSthorpej 			if (this_inblock) {
18246e45676fSthorpej 				if (params->flags & IB_VERBOSE) {
18256e45676fSthorpej 					printf("(Reading '%s' -- %zu @ %lld)\n",
18266e45676fSthorpej 					       uboot_file_name,
18276e45676fSthorpej 					       this_inblock,
18286e45676fSthorpej 					       (long long)lseek(ifd, 0,
18296e45676fSthorpej 								SEEK_CUR));
18306e45676fSthorpej 				}
18316e45676fSthorpej 				if (read(ifd, fill, this_inblock)
18326e45676fSthorpej 				    != (ssize_t)this_inblock) {
183302d67d10Sthorpej 					warn("read '%s'", uboot_file);
183402d67d10Sthorpej 					goto out;
183502d67d10Sthorpej 				}
18366e45676fSthorpej 				file_remaining -= this_inblock;
18376e45676fSthorpej 			}
18386e45676fSthorpej 		}
18396e45676fSthorpej 
18406e45676fSthorpej 		if (params->flags & IB_VERBOSE) {
18416e45676fSthorpej 			printf("(Writing '%s' -- %zu @ %lld)\n",
18426e45676fSthorpej 			       params->filesystem,
18436e45676fSthorpej 			       output_block_size, (long long)curoffset);
18446e45676fSthorpej 		}
184502d67d10Sthorpej 		if (!(params->flags & IB_NOWRITE) &&
18466e45676fSthorpej 		    pwrite(params->fsfd, blockbuf, output_block_size,
18476e45676fSthorpej 			   curoffset) != (ssize_t)output_block_size) {
184802d67d10Sthorpej 			warn("pwrite '%s'", params->filesystem);
184902d67d10Sthorpej 			goto out;
185002d67d10Sthorpej 		}
185102d67d10Sthorpej 	}
185202d67d10Sthorpej 
185302d67d10Sthorpej 	/* Success! */
185402d67d10Sthorpej 	rv = true;
185502d67d10Sthorpej 
185602d67d10Sthorpej  out:
185702d67d10Sthorpej 	if (ifd != -1 && close(ifd) == -1)
185802d67d10Sthorpej 		warn("close '%s'", uboot_file);
185902d67d10Sthorpej 	if (blockbuf)
186002d67d10Sthorpej 		free(blockbuf);
186102d67d10Sthorpej 	return rv;
186202d67d10Sthorpej }
186302d67d10Sthorpej 
186402d67d10Sthorpej int
evb_uboot_setboot(ib_params * params,evb_board board)186502d67d10Sthorpej evb_uboot_setboot(ib_params *params, evb_board board)
186602d67d10Sthorpej {
186702d67d10Sthorpej 	char uboot_filebuf[PATH_MAX+1];
186802d67d10Sthorpej 	const char *uboot_file;
186902d67d10Sthorpej 	struct stat sb;
187002d67d10Sthorpej 	off_t max_offset = 0;
187102d67d10Sthorpej 
187202d67d10Sthorpej 	/*
187302d67d10Sthorpej 	 * If we don't have a u-boot path for this board, it means
187402d67d10Sthorpej 	 * that a u-boot package wasn't found.  Prompt the user to
187502d67d10Sthorpej 	 * install it.
187602d67d10Sthorpej 	 */
187702d67d10Sthorpej 	if (evb_board_get_uboot_path(params, board) == NULL) {
187802d67d10Sthorpej 		warnx("No u-boot package found for board '%s'",
187902d67d10Sthorpej 		    params->board);
188002d67d10Sthorpej 		uboot_file = evb_board_get_uboot_pkg(params, board);
188102d67d10Sthorpej 		if (uboot_file != NULL)
188202d67d10Sthorpej 			warnx("Please install the sysutils/u-boot-%s package.",
188302d67d10Sthorpej 			    uboot_file);
188402d67d10Sthorpej 		return 0;
188502d67d10Sthorpej 	}
188602d67d10Sthorpej 
188702d67d10Sthorpej 	evb_ubinstall install = evb_board_get_uboot_install(params, board);
188802d67d10Sthorpej 	evb_ubsteps steps;
188902d67d10Sthorpej 	evb_ubstep step;
189002d67d10Sthorpej 
189102d67d10Sthorpej 	if (install == NULL)
189202d67d10Sthorpej 		return 0;
189302d67d10Sthorpej 
189402d67d10Sthorpej 	/*
189502d67d10Sthorpej 	 * First, make sure the files are all there.  While we're
189602d67d10Sthorpej 	 * at it, calculate the largest byte offset that we will
189702d67d10Sthorpej 	 * be writing.
189802d67d10Sthorpej 	 */
189902d67d10Sthorpej 	steps = evb_ubinstall_get_steps(params, install);
190002d67d10Sthorpej 	while ((step = evb_ubsteps_next_step(params, steps)) != NULL) {
190102d67d10Sthorpej 		uint64_t file_offset = evb_ubstep_get_file_offset(params, step);
190202d67d10Sthorpej 		uint64_t file_size = evb_ubstep_get_file_size(params, step);
190302d67d10Sthorpej 		uint64_t image_offset =
190402d67d10Sthorpej 		    evb_ubstep_get_image_offset(params, step);
190502d67d10Sthorpej 		uboot_file = evb_uboot_file_path(params, board, step,
190602d67d10Sthorpej 		    uboot_filebuf, sizeof(uboot_filebuf));
190702d67d10Sthorpej 		if (uboot_file == NULL)
190802d67d10Sthorpej 			return 0;
190902d67d10Sthorpej 		if (stat(uboot_file, &sb) < 0) {
191002d67d10Sthorpej 			warn("%s", uboot_file);
191102d67d10Sthorpej 			return 0;
191202d67d10Sthorpej 		}
191302d67d10Sthorpej 		if (!S_ISREG(sb.st_mode)) {
191402d67d10Sthorpej 			warnx("%s: %s", uboot_file, strerror(EFTYPE));
191502d67d10Sthorpej 			return 0;
191602d67d10Sthorpej 		}
191702d67d10Sthorpej 		off_t this_max;
191802d67d10Sthorpej 		if (file_size)
191902d67d10Sthorpej 			this_max = file_size;
192002d67d10Sthorpej 		else
192102d67d10Sthorpej 			this_max = sb.st_size - file_offset;
192202d67d10Sthorpej 		this_max += image_offset;
192302d67d10Sthorpej 		if (max_offset < this_max)
192402d67d10Sthorpej 			max_offset = this_max;
192502d67d10Sthorpej 	}
192602d67d10Sthorpej 
192702d67d10Sthorpej 	/*
192802d67d10Sthorpej 	 * Ok, we've verified that all of the files are there, and now
192902d67d10Sthorpej 	 * max_offset points to the first byte that's available for a
193002d67d10Sthorpej 	 * partition containing a file system.
193102d67d10Sthorpej 	 */
193202d67d10Sthorpej 
193302d67d10Sthorpej 	off_t rounded_max_offset = (off_t)(max_offset / params->sectorsize) *
193402d67d10Sthorpej 	    params->sectorsize;
193502d67d10Sthorpej 	if (rounded_max_offset != max_offset)
193602d67d10Sthorpej 		rounded_max_offset += params->sectorsize;
193702d67d10Sthorpej 
193802d67d10Sthorpej 	if (params->flags & IB_VERBOSE) {
193902d67d10Sthorpej 		printf("Max u-boot offset (rounded): %lld (%lld)\n",
194002d67d10Sthorpej 		    (long long)max_offset, (long long)rounded_max_offset);
194102d67d10Sthorpej 		printf("First free block available for file systems: "
194202d67d10Sthorpej 		    "%lld (0x%llx)\n",
194302d67d10Sthorpej 		    (long long)rounded_max_offset / params->sectorsize,
194402d67d10Sthorpej 		    (long long)rounded_max_offset / params->sectorsize);
194502d67d10Sthorpej 	}
194602d67d10Sthorpej 
194702d67d10Sthorpej 	/* XXX Check MBR table for overlapping partitions. */
194802d67d10Sthorpej 
194902d67d10Sthorpej 	/*
195002d67d10Sthorpej 	 * Now write each binary component to the appropriate location
195102d67d10Sthorpej 	 * on disk.
195202d67d10Sthorpej 	 */
195302d67d10Sthorpej 	steps = evb_ubinstall_get_steps(params, install);
195402d67d10Sthorpej 	while ((step = evb_ubsteps_next_step(params, steps)) != NULL) {
195502d67d10Sthorpej 		uboot_file = evb_uboot_file_path(params, board, step,
195602d67d10Sthorpej 		    uboot_filebuf, sizeof(uboot_filebuf));
195702d67d10Sthorpej 		if (uboot_file == NULL)
195802d67d10Sthorpej 			return 0;
195902d67d10Sthorpej 		if (!evb_uboot_do_step(params, uboot_file, step))
196002d67d10Sthorpej 			return 0;
196102d67d10Sthorpej 	}
196202d67d10Sthorpej 
196302d67d10Sthorpej 	return 1;
196402d67d10Sthorpej }
1965