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