write_sparc64_disk.c revision 106232
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 106232 2002-10-31 04:25:17Z 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	sl = calloc(sizeof *sl, 1);
40	c = d1->chunks;
41	c2 = c->part;
42	secpercyl = d1->bios_sect * d1->bios_hd;
43	sl->sl_pcylinders = c->size / secpercyl;
44	sl->sl_ncylinders = c2->size / secpercyl;
45	sl->sl_acylinders = sl->sl_pcylinders - sl->sl_ncylinders;
46	sl->sl_magic = SUN_DKMAGIC;
47	sl->sl_nsectors = d1->bios_sect;
48	sl->sl_ntracks = d1->bios_hd;
49	if (c->size > 4999 * 1024 * 2) {
50		sprintf(sl->sl_text, "FreeBSD%luG cyl %u alt %u hd %u sec %u",
51		    (c->size + 1024 * 1024) / (2 * 1024 * 1024),
52		    sl->sl_ncylinders, sl->sl_acylinders,
53		    sl->sl_ntracks, sl->sl_nsectors);
54	} else {
55		sprintf(sl->sl_text, "FreeBSD%luM cyl %u alt %u hd %u sec %u",
56		    (c->size + 1024) / (2 * 1024),
57		    sl->sl_ncylinders, sl->sl_acylinders,
58		    sl->sl_ntracks, sl->sl_nsectors);
59	}
60	sl->sl_interleave = 1;
61	sl->sl_sparespercyl = 0;
62	sl->sl_rpm = 3600;
63
64	for (c1 = c2->part; c1 != NULL; c1 = c1->next) {
65		p = c1->name;
66		p += strlen(p);
67		p--;
68		if (*p < 'a' || *p > 'h')
69			continue;
70		i = *p - 'a';
71		sl->sl_part[i].sdkp_cyloffset = c1->offset / secpercyl;
72		sl->sl_part[i].sdkp_nsectors = c1->size;
73	}
74
75	sp1 = (u_short *)sl;
76	sp2 = (u_short *)(sl + 1);
77	sl->sl_cksum = cksum = 0;
78	while (sp1 < sp2)
79		cksum ^= *sp1++;
80	sl->sl_cksum = cksum;
81
82	strcpy(device,_PATH_DEV);
83        strcat(device,d1->name);
84
85        fd = open(device,O_RDWR);
86        if (fd < 0) {
87                warn("open(%s) failed", device);
88                return (1);
89        }
90
91	write_block(fd, 0, sl, sizeof *sl);
92
93	for (i = 1; i < 16; i++)
94		write_block(fd, i, d1->boot1 + (i * 512), 512);
95
96	close(fd);
97	return 0;
98}
99