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