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