Home | History | Annotate | Line # | Download | only in wrtvid
wrtvid.c revision 1.6.94.1
      1  1.6.94.1    mjf /*	$NetBSD: wrtvid.c,v 1.6.94.1 2008/02/18 21:04:52 mjf 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.6.94.1    mjf write32(uint8_t *vp, uint32_t value)
     93       1.5    scw {
     94  1.6.94.1    mjf 
     95  1.6.94.1    mjf 	*vp++ = (uint8_t)((value >> 24) & 0xff);
     96  1.6.94.1    mjf 	*vp++ = (uint8_t)((value >> 16) & 0xff);
     97  1.6.94.1    mjf 	*vp++ = (uint8_t)((value >> 8) & 0xff);
     98  1.6.94.1    mjf 	*vp = (uint8_t)(value & 0xff);
     99       1.1  chuck }
    100       1.1  chuck 
    101       1.5    scw /*
    102       1.5    scw  * Write a big-endian 16-bit value at the specified address
    103       1.5    scw  */
    104       1.2    scw static void
    105  1.6.94.1    mjf write16(uint8_t *vp, uint16_t value)
    106       1.1  chuck {
    107  1.6.94.1    mjf 
    108  1.6.94.1    mjf 	*vp++ = (uint8_t)((value >> 8) & 0xff);
    109  1.6.94.1    mjf 	*vp = (uint8_t)(value & 0xff);
    110       1.1  chuck }
    111       1.1  chuck 
    112       1.5    scw int
    113       1.5    scw main(int argc, char **argv)
    114       1.1  chuck {
    115       1.5    scw 	struct stat st;
    116  1.6.94.1    mjf 	uint16_t len;
    117  1.6.94.1    mjf 	uint8_t *vid;
    118       1.5    scw 	char *fn;
    119       1.5    scw 	int is_disk;
    120       1.5    scw 	int fd;
    121       1.5    scw 
    122       1.5    scw 	if (argc != 2) {
    123       1.5    scw usage:
    124       1.5    scw 		fprintf(stderr, "%s: <bootsd|bootst>\n", argv[0]);
    125       1.5    scw 		exit(1);
    126       1.5    scw 	}
    127       1.5    scw 
    128       1.5    scw 	if (strcmp(argv[1], "bootsd") == 0) {
    129       1.5    scw 		is_disk = 1;
    130       1.5    scw 		fn = "sdboot";
    131       1.5    scw 	} else
    132       1.5    scw 	if (strcmp(argv[1], "bootst") == 0) {
    133       1.5    scw 		is_disk = 0;
    134       1.5    scw 		fn = "stboot";
    135       1.5    scw 	} else
    136       1.5    scw 		goto usage;
    137       1.5    scw 
    138       1.5    scw 	if (stat(argv[1], &st) < 0) {
    139       1.5    scw 		perror(argv[1]);
    140       1.5    scw 		exit(1);
    141       1.5    scw 	}
    142       1.5    scw 
    143       1.5    scw 	/* How many 256-byte logical blocks (rounded up) */
    144  1.6.94.1    mjf 	len = (uint16_t)((st.st_size + 255) / 256);
    145       1.6    scw 
    146       1.6    scw 	/* For tapes, round up to 8k */
    147       1.6    scw 	if (is_disk == 0) {
    148       1.6    scw 		len += (8192 / 256) - 1;
    149       1.6    scw 		len &= ~((8192 / 256) -1);
    150       1.6    scw 	}
    151       1.5    scw 
    152       1.5    scw 	if ((vid = malloc(SIZEOF_VID)) == NULL) {
    153       1.5    scw 		perror(argv[0]);
    154       1.5    scw 		exit(1);
    155       1.5    scw 	}
    156       1.5    scw 
    157       1.5    scw 	/* Generate the VID and CFG data */
    158       1.5    scw 	memset(vid, 0, SIZEOF_VID);
    159       1.5    scw 	memcpy(&vid[VID_ID_OFF], VID_ID, VID_ID_LEN);
    160       1.5    scw 	write32(&vid[VID_OSS_OFF], is_disk ? VID_OSS_DISK : VID_OSS_TAPE);
    161       1.5    scw 	write16(&vid[VID_OSL_OFF], len);
    162       1.5    scw 	write32(&vid[VID_OSA_OFF], VID_OSA);
    163       1.5    scw 	vid[VID_CAS_OFF] = VID_CAS;
    164       1.5    scw 	vid[VID_CAL_OFF] = VID_CAL;
    165       1.5    scw 	memcpy(&vid[VID_MOT_OFF], VID_MOT, VID_MOT_LEN);
    166       1.5    scw 	write16(&vid[CFG_REC_OFF], CFG_REC);
    167       1.5    scw 	write16(&vid[CFG_PSM_OFF], CFG_PSM);
    168       1.5    scw 
    169       1.5    scw 	/* Create and write it out */
    170       1.5    scw 	if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) {
    171       1.5    scw 		perror(fn);
    172       1.5    scw 		exit(1);
    173       1.5    scw 	}
    174       1.5    scw 
    175       1.5    scw 	if (write(fd, vid, SIZEOF_VID) != SIZEOF_VID) {
    176       1.5    scw 		perror(fn);
    177       1.5    scw 		exit(1);
    178       1.5    scw 	}
    179       1.5    scw 
    180       1.5    scw 	close(fd);
    181       1.5    scw 	free(vid);
    182       1.5    scw 
    183  1.6.94.1    mjf 	return 0;
    184       1.1  chuck }
    185