write_sparc64_disk.c revision 106745
1/*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
8 */
9
10#include <sys/cdefs.h>
11__FBSDID("$FreeBSD: head/lib/libdisk/write_sparc64_disk.c 106745 2002-11-10 21:07:29Z jake $");
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <fcntl.h>
17#include <err.h>
18#include <string.h>
19#include <sys/types.h>
20#include <sys/stat.h>
21#include <sys/ioctl.h>
22#include <sys/sun_disklabel.h>
23#include <paths.h>
24#include <errno.h>
25#include "libdisk.h"
26
27int
28Write_Disk(const struct disk *d1)
29{
30	struct sun_disklabel *sl;
31	struct chunk *c, *c1, *c2;
32	int i;
33	char *p;
34	u_long secpercyl;
35	u_short *sp1, *sp2, cksum;
36	char device[64];
37	int fd;
38
39	strcpy(device,_PATH_DEV);
40	strcat(device,d1->name);
41
42	fd = open(device,O_RDWR);
43	if (fd < 0) {
44		warn("open(%s) failed", device);
45		return (1);
46	}
47
48	sl = calloc(sizeof *sl, 1);
49	c = d1->chunks;
50	c2 = c->part;
51	secpercyl = d1->bios_sect * d1->bios_hd;
52	sl->sl_pcylinders = c->size / secpercyl;
53	sl->sl_ncylinders = c2->size / secpercyl;
54	sl->sl_acylinders = sl->sl_pcylinders - sl->sl_ncylinders;
55	sl->sl_magic = SUN_DKMAGIC;
56	sl->sl_nsectors = d1->bios_sect;
57	sl->sl_ntracks = d1->bios_hd;
58	if (c->size > 4999 * 1024 * 2) {
59		sprintf(sl->sl_text, "FreeBSD%luG cyl %u alt %u hd %u sec %u",
60		    (c->size + 1024 * 1024) / (2 * 1024 * 1024),
61		    sl->sl_ncylinders, sl->sl_acylinders,
62		    sl->sl_ntracks, sl->sl_nsectors);
63	} else {
64		sprintf(sl->sl_text, "FreeBSD%luM cyl %u alt %u hd %u sec %u",
65		    (c->size + 1024) / (2 * 1024),
66		    sl->sl_ncylinders, sl->sl_acylinders,
67		    sl->sl_ntracks, sl->sl_nsectors);
68	}
69	sl->sl_interleave = 1;
70	sl->sl_sparespercyl = 0;
71	sl->sl_rpm = 3600;
72
73	for (c1 = c2->part; c1 != NULL; c1 = c1->next) {
74		p = c1->name;
75		p += strlen(p);
76		p--;
77		if (*p < 'a' || *p > 'h')
78			continue;
79		i = *p - 'a';
80		sl->sl_part[i].sdkp_cyloffset = c1->offset / secpercyl;
81		sl->sl_part[i].sdkp_nsectors = c1->size;
82		for (i = 1; i < 16; i++) {
83			write_block(fd, c1->offset + i, d1->boot1 + (i * 512),
84			    512);
85		}
86	}
87
88	/*
89	 * We need to fill in the "RAW" partition as well.  Emperical data
90	 * seems to indicate that this covers the "obviously" visible part
91	 * of the disk, ie: sl->sl_ncylinders.
92	 */
93	sl->sl_part[2].sdkp_cyloffset = 0;
94	sl->sl_part[2].sdkp_nsectors = sl->sl_ncylinders * secpercyl;
95
96	sp1 = (u_short *)sl;
97	sp2 = (u_short *)(sl + 1);
98	sl->sl_cksum = cksum = 0;
99	while (sp1 < sp2)
100		cksum ^= *sp1++;
101	sl->sl_cksum = cksum;
102
103	write_block(fd, 0, sl, sizeof *sl);
104
105	close(fd);
106	return 0;
107}
108