wrtvid.c revision 1.7 1 /* $NetBSD: wrtvid.c,v 1.7 2008/01/12 09:54:33 tsutsui Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Steve C. Woodford.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 /* mvme68k's boot block is 512 bytes long */
47 #define SIZEOF_VID 0x200
48
49 /* The first field is effectively the vendor string, in this case NBSD */
50 #define VID_ID_OFF 0x000
51 #define VID_ID "NBSD"
52 #define VID_ID_LEN 4
53
54 /* Start block for the 1st stage bootstrap code */
55 #define VID_OSS_OFF 0x014
56 #define VID_OSS_TAPE 1
57 #define VID_OSS_DISK 2
58
59 /* Length, in 256-byte logical blocks, of the 1st stage bootstrap */
60 #define VID_OSL_OFF 0x018
61
62 /* Start address of the bootstrap */
63 #define VID_OSA_OFF 0x01e
64 #define VID_OSA 0x003f0000
65
66 /* Block number of config area */
67 #define VID_CAS_OFF 0x093
68 #define VID_CAS 1
69
70 /* Length, in 256-byte logical blocks, of config area */
71 #define VID_CAL_OFF 0x094
72 #define VID_CAL 1
73
74 /* Magic `MOTOROLA' string */
75 #define VID_MOT_OFF 0x0f8
76 #define VID_MOT "MOTOROLA"
77 #define VID_MOT_LEN 8
78
79 /* Logical block size, in bytes */
80 #define CFG_REC_OFF 0x10a
81 #define CFG_REC 0x100
82
83 /* Physical sector size, in bytes */
84 #define CFG_PSM_OFF 0x11e
85 #define CFG_PSM 0x200
86
87
88 /*
89 * Write a big-endian 32-bit value at the specified address
90 */
91 static void
92 write32(uint8_t *vp, uint32_t value)
93 {
94
95 *vp++ = (uint8_t)((value >> 24) & 0xff);
96 *vp++ = (uint8_t)((value >> 16) & 0xff);
97 *vp++ = (uint8_t)((value >> 8) & 0xff);
98 *vp = (uint8_t)(value & 0xff);
99 }
100
101 /*
102 * Write a big-endian 16-bit value at the specified address
103 */
104 static void
105 write16(uint8_t *vp, uint16_t value)
106 {
107
108 *vp++ = (uint8_t)((value >> 8) & 0xff);
109 *vp = (uint8_t)(value & 0xff);
110 }
111
112 int
113 main(int argc, char **argv)
114 {
115 struct stat st;
116 uint16_t len;
117 uint8_t *vid;
118 char *fn;
119 int is_disk;
120 int fd;
121
122 if (argc != 2) {
123 usage:
124 fprintf(stderr, "%s: <bootsd|bootst>\n", argv[0]);
125 exit(1);
126 }
127
128 if (strcmp(argv[1], "bootsd") == 0) {
129 is_disk = 1;
130 fn = "sdboot";
131 } else
132 if (strcmp(argv[1], "bootst") == 0) {
133 is_disk = 0;
134 fn = "stboot";
135 } else
136 goto usage;
137
138 if (stat(argv[1], &st) < 0) {
139 perror(argv[1]);
140 exit(1);
141 }
142
143 /* How many 256-byte logical blocks (rounded up) */
144 len = (uint16_t)((st.st_size + 255) / 256);
145
146 /* For tapes, round up to 8k */
147 if (is_disk == 0) {
148 len += (8192 / 256) - 1;
149 len &= ~((8192 / 256) -1);
150 }
151
152 if ((vid = malloc(SIZEOF_VID)) == NULL) {
153 perror(argv[0]);
154 exit(1);
155 }
156
157 /* Generate the VID and CFG data */
158 memset(vid, 0, SIZEOF_VID);
159 memcpy(&vid[VID_ID_OFF], VID_ID, VID_ID_LEN);
160 write32(&vid[VID_OSS_OFF], is_disk ? VID_OSS_DISK : VID_OSS_TAPE);
161 write16(&vid[VID_OSL_OFF], len);
162 write32(&vid[VID_OSA_OFF], VID_OSA);
163 vid[VID_CAS_OFF] = VID_CAS;
164 vid[VID_CAL_OFF] = VID_CAL;
165 memcpy(&vid[VID_MOT_OFF], VID_MOT, VID_MOT_LEN);
166 write16(&vid[CFG_REC_OFF], CFG_REC);
167 write16(&vid[CFG_PSM_OFF], CFG_PSM);
168
169 /* Create and write it out */
170 if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) {
171 perror(fn);
172 exit(1);
173 }
174
175 if (write(fd, vid, SIZEOF_VID) != SIZEOF_VID) {
176 perror(fn);
177 exit(1);
178 }
179
180 close(fd);
181 free(vid);
182
183 return 0;
184 }
185