1 /* $NetBSD: hp300.c,v 1.15 2013/06/14 03:54:43 msaitoh Exp $ */
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: hp300.c,v 1.15 2013/06/14 03:54:43 msaitoh Exp $");
41 /* We need the target disklabel.h, not the hosts one..... */
42 #ifdef HAVE_NBTOOL_CONFIG_H
43 #include "nbtool_config.h"
44 #include <nbinclude/hp300/disklabel.h>
45 #include <nbinclude/sys/disklabel.h>
47 #include <sys/disklabel.h>
49 #include <sys/fcntl.h>
50 #include <sys/ioctl.h>
52 #include <sys/param.h>
63 #include "installboot.h"
65 static int hp300_setboot(ib_params
*);
67 struct ib_mach ib_mach_hp300
=
68 { "hp300", hp300_setboot
, no_clearboot
, no_editboot
, IB_APPEND
};
71 hp300_setboot(ib_params
*params
)
76 struct partition
*boot
;
77 struct hp300_lifdir
*lifdir
;
80 unsigned int secsize
= HP300_SECTSIZE
;
81 uint64_t boot_size
, boot_offset
;
82 struct disklabel
*label
;
84 assert(params
!= NULL
);
85 assert(params
->fsfd
!= -1);
86 assert(params
->filesystem
!= NULL
);
87 assert(params
->s1fd
!= -1);
88 assert(params
->stage1
!= NULL
);
91 bootstrap
= MAP_FAILED
;
93 label
= malloc(params
->sectorsize
);
95 warn("Failed to allocate memory for disklabel");
99 if (params
->flags
& IB_APPEND
) {
100 if (!S_ISREG(params
->fsstat
.st_mode
)) {
102 "`%s' must be a regular file to append a bootstrap",
106 boot_offset
= roundup(params
->fsstat
.st_size
, HP300_SECTSIZE
);
109 * The bootstrap can be well over 8k, and must go into a BOOT
110 * partition. Read NetBSD label to locate BOOT partition.
112 if (pread(params
->fsfd
, label
, params
->sectorsize
,
113 LABELSECTOR
* params
->sectorsize
)
114 != (ssize_t
)params
->sectorsize
) {
115 warn("reading disklabel");
118 /* And a quick validation - must be a big-endian label */
119 secsize
= be32toh(label
->d_secsize
);
120 if (label
->d_magic
!= htobe32(DISKMAGIC
) ||
121 label
->d_magic2
!= htobe32(DISKMAGIC
) ||
122 secsize
== 0 || secsize
& (secsize
- 1) ||
123 be16toh(label
->d_npartitions
) > MAXMAXPARTITIONS
) {
124 warnx("Invalid disklabel in %s", params
->filesystem
);
128 i
= be16toh(label
->d_npartitions
);
129 for (boot
= label
->d_partitions
; ; boot
++) {
131 warnx("No BOOT partition");
134 if (boot
->p_fstype
== FS_BOOT
)
137 boot_size
= be32toh(boot
->p_size
) * (uint64_t)secsize
;
138 boot_offset
= be32toh(boot
->p_offset
) * (uint64_t)secsize
;
141 * We put the entire LIF file into the BOOT partition even when
142 * it doesn't start at the beginning of the disk.
144 * Maybe we ought to be able to take a binary file and add
145 * it to the LIF filesystem.
147 if (boot_size
< (uint64_t)params
->s1stat
.st_size
) {
148 warn("BOOT partition too small (%llu < %llu)",
149 (unsigned long long)boot_size
,
150 (unsigned long long)params
->s1stat
.st_size
);
155 bootstrap
= mmap(NULL
, params
->s1stat
.st_size
, PROT_READ
| PROT_WRITE
,
156 MAP_PRIVATE
, params
->s1fd
, 0);
157 if (bootstrap
== MAP_FAILED
) {
158 warn("mmaping `%s'", params
->stage1
);
162 /* Relocate files, sanity check LIF directory on the way */
163 lifdir
= (void *)(bootstrap
+ HP300_SECTSIZE
* 2);
164 for (i
= 0; i
< 8; lifdir
++, i
++) {
165 int32_t addr
= be32toh(lifdir
->dir_addr
);
166 int32_t limit
= (params
->s1stat
.st_size
- 1) / HP300_SECTSIZE
+ 1;
167 int32_t end
= addr
+ be32toh(lifdir
->dir_length
);
169 warnx("LIF entry %d larger (%d %d) than LIF file",
173 if (addr
!= 0 && boot_offset
!= 0)
174 lifdir
->dir_addr
= htobe32(addr
+ boot_offset
178 if (params
->flags
& IB_NOWRITE
) {
183 /* Write LIF volume header and directory to sectors 0 and 1 */
184 rv
= pwrite(params
->fsfd
, bootstrap
, 1024, 0);
187 warn("Writing `%s'", params
->filesystem
);
189 warnx("Writing `%s': short write", params
->filesystem
);
193 /* Write files to BOOT partition */
194 offset
= boot_offset
<= HP300_SECTSIZE
* 16 ? HP300_SECTSIZE
* 16 : 0;
195 i
= roundup(params
->s1stat
.st_size
, secsize
) - offset
;
196 rv
= pwrite(params
->fsfd
, bootstrap
+ offset
, i
, boot_offset
+ offset
);
199 warn("Writing boot filesystem of `%s'",
202 warnx("Writing boot filesystem of `%s': short write",
212 if (bootstrap
!= MAP_FAILED
)
213 munmap(bootstrap
, params
->s1stat
.st_size
);