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