Home | History | Annotate | Line # | Download | only in boot
disklabel.c revision 1.1
      1 /*	$NetBSD: disklabel.c,v 1.1 2013/01/05 17:44:24 tsutsui Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992 OMRON Corporation.
      5  *
      6  * This code is derived from software contributed to Berkeley by
      7  * OMRON Corporation.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Berkeley and its contributors.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  *
     37  *	@(#)disklabel.c	8.1 (Berkeley) 6/10/93
     38  */
     39 /*
     40  * Copyright (c) 1992, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * OMRON Corporation.
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)disklabel.c	8.1 (Berkeley) 6/10/93
     71  */
     72 
     73 /*
     74  * disklabel.c -- operate disklabel for BSD & OMRON
     75  * by A.Fujita, FEB-17-1992
     76  */
     77 
     78 #include <sys/param.h>
     79 #define DKTYPENAMES
     80 #define FSTYPENAMES
     81 #include <sys/disklabel.h>
     82 #include <ufs/ffs/fs.h>
     83 #include <lib/libkern/libkern.h>
     84 #include <luna68k/stand/boot/samachdep.h>
     85 #include <luna68k/stand/boot/status.h>
     86 #include <luna68k/stand/boot/omron_disklabel.h>
     87 
     88 static void display(struct disklabel *);
     89 
     90 #define SBSIZE SBLOCKSIZE
     91 #define FS_MAGIC FS_UFS1_MAGIC
     92 #define LABEL_SIZE BBSIZE
     93 
     94 u_char lbl_buff[LABEL_SIZE];
     95 
     96 #if 0
     97 u_short
     98 dkcksum(struct disklabel *lp)
     99 {
    100 	u_short *start, *end;
    101 	u_short sum = 0;
    102 
    103 	start = (u_short *)lp;
    104 	end = (u_short *)&lp->d_partitions[lp->d_npartitions];
    105 	while (start < end)
    106 		sum ^= *start++;
    107 	return (sum);
    108 }
    109 #endif
    110 
    111 int
    112 disklabel(int argc, char *argv[])
    113 {
    114 	struct scd_dk_label *omp = (struct scd_dk_label *) lbl_buff;
    115 	struct disklabel    *bp  = (struct disklabel *)omp->dkl_pad;
    116 	struct fs *fp = (struct fs *) lbl_buff;
    117 	u_short *p;
    118 	u_long chksum, count;
    119 	char *q;
    120 	int i, j;
    121 
    122 	if (argc < 2) {
    123 		printf("This command is required sub command !!\n");
    124 		return(ST_ERROR);
    125 	}
    126 
    127 	if (!strcmp(argv[1], "help")) {
    128 		printf("Subcommand of disklabel\n\n");
    129 		printf("\thelp:\t\tthis command\n");
    130 		printf("\tread:\t\tread disklabel from scsi_device\n");
    131 		printf("\twrite:\t\twrite disklabel to scsi_device\n");
    132 		printf("\tomron:\t\tshow OMRON disklabel infomation\n");
    133 		printf("\tbsd:\t\tshow BSD disklabel infomation\n");
    134 		printf("\tcopy:\t\tcopy disklabel infomation from OMRON to BSD\n");
    135 		printf("\tchecksum:\tdoing checksum\n");
    136 		printf("\tset:\t\tchange BSD disklabel infomation\n");
    137 		printf("\n\n");
    138 	} else if (!strcmp(argv[1], "read")) {
    139 		if (scsi_read( 0, lbl_buff, LABEL_SIZE)) {
    140 			printf("Disk Label read done.\n");
    141 		} else {
    142 			printf("Disk Label read error !!\n");
    143 		}
    144 	} else if (!strcmp(argv[1], "omron")) {
    145 		i  = (int) &omp->dkl_badchk;
    146 		i -= (int) lbl_buff;
    147 		printf("Offset = %d\n", i);
    148 		printf("\n");
    149 		printf("Checksum of Bad Track:\t0x%x\n",	omp->dkl_badchk);
    150 		printf("Logical Block Total:\t%lu(0x%lx)\n",	omp->dkl_maxblk, omp->dkl_maxblk);
    151 		printf("Disk Drive Type:\t0x%x\n",		omp->dkl_dtype);
    152 		printf("Number of Disk Drives:\t%d(0x%x)\n",	omp->dkl_ndisk, omp->dkl_ndisk);
    153 		printf("Number of Data Cylinders:\t%d(0x%x)\n",	omp->dkl_ncyl, omp->dkl_ncyl);
    154 		printf("Number of Alternate Cylinders:\t%d(0x%x)\n",
    155 		       omp->dkl_acyl,omp->dkl_acyl);
    156 		printf("Number of Heads in This Partition:\t%d(0x%x)\n",
    157 		       omp->dkl_nhead, omp->dkl_nhead);
    158 		printf("Number of 512 byte Sectors per Track:\t%d(0x%x)\n",
    159 		       omp->dkl_nsect, omp->dkl_nsect);
    160 		printf("Identifies Proper Label Locations:\t0x%x\n",
    161 		       omp->dkl_bhead);
    162 		printf("Physical Partition Number:\t%d(0x%x)\n",
    163 		       omp->dkl_ppart, omp->dkl_ppart);
    164 		for (i = 0; i < NLPART; i++)
    165 			printf("\t%d:\t%ld\t%ld\n", i,
    166 			       (long)omp->dkl_map[i].dkl_blkno, (long)omp->dkl_map[i].dkl_nblk);
    167 		printf("Identifies This Label Format:\t0x%x\n",	omp->dkl_magic);
    168 		printf("XOR Checksum of Sector:\t0x%x\n",	omp->dkl_cksum);
    169 	} else if (!strcmp(argv[1], "checksum")) {
    170 		if (omp->dkl_magic == DKL_MAGIC){
    171 							/* checksum of disk-label */
    172 			chksum = 0;
    173 			count = sizeof(struct scd_dk_label) / sizeof(short int);
    174 			for (p= (u_short *) lbl_buff; count > 0; count--) {
    175 				if (count == 1)
    176 					printf("Check Sum: 0x%lx\n", chksum);
    177 				chksum ^= *p++;
    178 			}
    179 
    180 			printf("dkl_cksum: 0x%x\n", omp->dkl_cksum);
    181 
    182 			if (chksum != 0) {
    183 				printf("OMRON Disklabel check sum error.\n");
    184 			}
    185 		} else {
    186 			printf("OMRON Disklabel not found.\n");
    187 		}
    188 	} else if (!strcmp(argv[1], "copy")) {
    189 		memset(bp, 0, sizeof(struct disklabel));
    190 
    191 		memcpy(bp->d_typename, lbl_buff, 16);
    192 
    193 		bp->d_secsize    = DEV_BSIZE;
    194 		bp->d_nsectors   = 38;
    195 		bp->d_ntracks    = 12;
    196 		bp->d_ncylinders = 1076;
    197 
    198 		bp->d_type  = DTYPE_SCSI;
    199 
    200 		bp->d_secpercyl  = bp->d_nsectors * bp->d_ntracks;
    201 		bp->d_secperunit = bp->d_secpercyl * bp->d_ncylinders;
    202 		bp->d_rpm        = 3600;
    203 		bp->d_interleave = 1;
    204 		bp->d_trackskew  = 0;
    205 		bp->d_cylskew    = 0;
    206 		bp->d_headswitch = 0;
    207 		bp->d_trkseek    = 0;
    208 		bp->d_bbsize     = BBSIZE;
    209 		bp->d_sbsize     = SBSIZE;
    210 
    211 		for (i = 0; i < MAXPARTITIONS; i++) {
    212 			bp->d_partitions[i].p_size   = omp->dkl_map[i].dkl_nblk;
    213 			bp->d_partitions[i].p_offset = omp->dkl_map[i].dkl_blkno;
    214 			bp->d_partitions[i].p_fsize  = 1024;
    215 			bp->d_partitions[i].p_frag   = 8192 / 1024;
    216 			bp->d_partitions[i].p_fstype = FS_UNUSED;
    217 		}
    218 
    219 		bp->d_npartitions = MAXPARTITIONS;
    220 
    221 		for (i = 0; i < NDDATA; i++) {
    222 			bp->d_drivedata[i] = 0;
    223 		}
    224 
    225 		memset(bp->d_packname, 0, 16);
    226 
    227 		bp->d_magic    = DISKMAGIC;
    228 		bp->d_magic2   = DISKMAGIC;
    229 		bp->d_checksum = 0;
    230 		bp->d_checksum = dkcksum(bp);
    231 
    232 		/* restump checksum of OMRON disklabel */
    233 		chksum = 0;
    234 		count = sizeof(struct scd_dk_label) / sizeof(short int);
    235 		for (p= (u_short *) lbl_buff; count > 1; count--) {
    236 			chksum ^= *p++;
    237 		}
    238 		printf("chksum: 0x%lx\n", chksum);
    239 
    240 		omp->dkl_cksum = chksum;
    241 		printf("dkl_cksum: 0x%x\n", omp->dkl_cksum);
    242 	} else if (!strcmp(argv[1], "bsd")) {
    243 		display(bp);
    244 	} else if (!strcmp(argv[1], "write")) {
    245 		if (scsi_write( 0, lbl_buff, LABEL_SIZE)) {
    246 			printf("Disk Label write done.\n");
    247 		} else {
    248 			printf("Disk Label write error !!\n");
    249 		}
    250 	} else if (!strcmp(argv[1], "set")) {
    251 		i = (argv[2])[1] - 'a';
    252 		for (q = argv[3], j = 0; *q != '\0'; q++) {
    253 			j = (j * 10) + (*q - '0');
    254 		}
    255 		switch (*argv[2]) {
    256 		case 'b':
    257 			bp->d_partitions[i].p_frag = j / bp->d_partitions[i].p_fsize;
    258 			break;
    259 		case 'f':	/* fragment size */
    260 			bp->d_partitions[i].p_fsize = j;
    261 			break;
    262 		case 'o':	/* offset */
    263 			bp->d_partitions[i].p_offset = j;
    264 			omp->dkl_map[i].dkl_blkno = j;
    265 			break;
    266 		case 'p':	/* size */
    267 			bp->d_partitions[i].p_size = j;
    268 			omp->dkl_map[i].dkl_nblk = j;
    269 			break;
    270 		case 't':	/* FS type */
    271 			bp->d_partitions[i].p_fstype = j;
    272 			break;
    273 		default:
    274 			break;
    275 		}
    276 
    277 		/* restump checksum of BSD disklabel */
    278 		bp->d_checksum = 0;
    279 		bp->d_checksum = dkcksum(bp);
    280 
    281 		/* restump checksum of OMRON disklabel */
    282 		chksum = 0;
    283 		count = sizeof(struct scd_dk_label) / sizeof(short int);
    284 		for (p= (u_short *) lbl_buff; count > 1; count--) {
    285 			chksum ^= *p++;
    286 		}
    287 		omp->dkl_cksum = chksum;
    288 
    289 	} else if (!strcmp(argv[1], "sb")) {
    290 #define BLOCK_SIZE	SBSIZE
    291 
    292 		printf("checking Super Block: block size = %d bytes, seek amount = 1 blocks\n",
    293 			BLOCK_SIZE);
    294 		i = j = 0;
    295 		while(1) {
    296 			if (!scsi_read( i, lbl_buff, BLOCK_SIZE))
    297 			break;
    298 
    299 			if (fp->fs_magic == FS_MAGIC) {
    300 				printf("%d, (%d)\n", i, i - j);
    301 				j = i;
    302 			}
    303 			i++;
    304 		}
    305 	} else if (!strcmp(argv[1], "sbcopy")) {
    306 		if (!scsi_read(32, lbl_buff, BLOCK_SIZE)) {
    307 			printf("sbcopy: read failed\n");
    308 			return(ST_ERROR);
    309 		}
    310 		if (scsi_write(16, lbl_buff, BLOCK_SIZE)) {
    311 			printf("sbcopy: copy done\n");
    312 		} else {
    313 			printf("sbcopy: write failed\n");
    314 		}
    315 	}
    316 
    317 	return(ST_NORMAL);
    318 }
    319 
    320 void
    321 display(struct disklabel *lp)
    322 {
    323 	int i, j;
    324 	struct partition *pp;
    325 
    326 	if ((unsigned) lp->d_type < DKMAXTYPES)
    327 		printf("type: %s\n", dktypenames[lp->d_type]);
    328 	else
    329 		printf("type: %d\n", lp->d_type);
    330 	printf("disk: %s\n",  lp->d_typename);
    331 	printf("label: %s\n", lp->d_packname);
    332 	printf("flags:");
    333 	if (lp->d_flags & D_REMOVABLE)
    334 		printf(" removeable");
    335 	if (lp->d_flags & D_ECC)
    336 		printf(" ecc");
    337 	if (lp->d_flags & D_BADSECT)
    338 		printf(" badsect");
    339 	printf("\n");
    340 	printf("bytes/sector: %d\n", lp->d_secsize);
    341 	printf("sectors/track: %d\n", lp->d_nsectors);
    342 	printf("tracks/cylinder: %d\n", lp->d_ntracks);
    343 	printf("sectors/cylinder: %d\n", lp->d_secpercyl);
    344 	printf("cylinders: %d\n", lp->d_ncylinders);
    345 	printf("rpm: %d\n", lp->d_rpm);
    346 	printf("interleave: %d\n", lp->d_interleave);
    347 	printf("trackskew: %d\n", lp->d_trackskew);
    348 	printf("cylinderskew: %d\n", lp->d_cylskew);
    349 	printf("headswitch: %d\t\t# milliseconds\n", lp->d_headswitch);
    350 	printf("track-to-track seek: %d\t# milliseconds\n", lp->d_trkseek);
    351 	printf("drivedata: ");
    352 	for (i = NDDATA - 1; i >= 0; i--)
    353 		if (lp->d_drivedata[i])
    354 			break;
    355 	if (i < 0)
    356 		i = 0;
    357 	for (j = 0; j <= i; j++)
    358 		printf("%d ", lp->d_drivedata[j]);
    359 	printf("\n\n%d partitions:\n", lp->d_npartitions);
    360 	printf("#        size   offset    fstype   [fsize bsize   cpg]\n");
    361 	pp = lp->d_partitions;
    362 	for (i = 0; i < lp->d_npartitions; i++, pp++) {
    363 		if (pp->p_size) {
    364 			printf("  %c: %d %d  ", 'a' + i,
    365 			   pp->p_size, pp->p_offset);
    366 			if ((unsigned) pp->p_fstype < FSMAXTYPES)
    367 				printf("%s", fstypenames[pp->p_fstype]);
    368 			else
    369 				printf("%d", pp->p_fstype);
    370 			switch (pp->p_fstype) {
    371 
    372 			case FS_UNUSED:				/* XXX */
    373 				printf("    %d %d %s ",
    374 				    pp->p_fsize, pp->p_fsize * pp->p_frag, "");
    375 				break;
    376 
    377 			case FS_BSDFFS:
    378 				printf("    %d %d %d ",
    379 				    pp->p_fsize, pp->p_fsize * pp->p_frag,
    380 				    pp->p_cpg);
    381 				break;
    382 
    383 			default:
    384 				printf("%s", "");
    385 				break;
    386 			}
    387 			printf("\t# (Cyl. %d",
    388 			    pp->p_offset / lp->d_secpercyl);
    389 			if (pp->p_offset % lp->d_secpercyl)
    390 			    cnputc('*');
    391 			else
    392 			    cnputc(' ');
    393 			printf("- %d",
    394 			    (pp->p_offset +
    395 			    pp->p_size + lp->d_secpercyl - 1) /
    396 			    lp->d_secpercyl - 1);
    397 			if (pp->p_size % lp->d_secpercyl)
    398 			    cnputc('*');
    399 			printf(")\n");
    400 		}
    401 	}
    402 }
    403