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