write_sparc64_disk.c revision 106303
1183873Sraj/*
2183873Sraj * ----------------------------------------------------------------------------
3183873Sraj * "THE BEER-WARE LICENSE" (Revision 42):
4183873Sraj * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5183873Sraj * can do whatever you want with this stuff. If we meet some day, and you think
6183873Sraj * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7183873Sraj * ----------------------------------------------------------------------------
8183873Sraj */
9183873Sraj
10191954Skuriyama#include <sys/cdefs.h>
11183873Sraj__FBSDID("$FreeBSD: head/lib/libdisk/write_sparc64_disk.c 106303 2002-11-01 16:28:19Z phk $");
12266328Sian
13183873Sraj#include <stdio.h>
14183873Sraj#include <stdlib.h>
15266328Sian#include <unistd.h>
16266328Sian#include <fcntl.h>
17266328Sian#include <err.h>
18266328Sian#include <string.h>
19266328Sian#include <sys/types.h>
20266331Sian#include <sys/stat.h>
21266328Sian#include <sys/ioctl.h>
22266328Sian#include <sys/sun_disklabel.h>
23266328Sian#include <paths.h>
24266328Sian#include <errno.h>
25191954Skuriyama#include "libdisk.h"
26191954Skuriyama
27191954Skuriyamaint
28191954SkuriyamaWrite_Disk(const struct disk *d1)
29183873Sraj{
30191954Skuriyama	struct sun_disklabel *sl;
31183873Sraj	struct chunk *c, *c1, *c2;
32266328Sian	int i;
33266328Sian	char *p;
34266328Sian	u_long secpercyl;
35266328Sian	u_short *sp1, *sp2, cksum;
36183873Sraj	char device[64];
37191954Skuriyama	int fd;
38183873Sraj
39183873Sraj	sl = calloc(sizeof *sl, 1);
40183873Sraj	c = d1->chunks;
41183873Sraj	c2 = c->part;
42191954Skuriyama	secpercyl = d1->bios_sect * d1->bios_hd;
43191954Skuriyama	sl->sl_pcylinders = c->size / secpercyl;
44266328Sian	sl->sl_ncylinders = c2->size / secpercyl;
45271339Sian	sl->sl_acylinders = sl->sl_pcylinders - sl->sl_ncylinders;
46266328Sian	sl->sl_magic = SUN_DKMAGIC;
47266328Sian	sl->sl_nsectors = d1->bios_sect;
48191954Skuriyama	sl->sl_ntracks = d1->bios_hd;
49271428Sian	if (c->size > 4999 * 1024 * 2) {
50271428Sian		sprintf(sl->sl_text, "FreeBSD%luG cyl %u alt %u hd %u sec %u",
51191954Skuriyama		    (c->size + 1024 * 1024) / (2 * 1024 * 1024),
52183873Sraj		    sl->sl_ncylinders, sl->sl_acylinders,
53185090Sraj		    sl->sl_ntracks, sl->sl_nsectors);
54185090Sraj	} else {
55183873Sraj		sprintf(sl->sl_text, "FreeBSD%luM cyl %u alt %u hd %u sec %u",
56183873Sraj		    (c->size + 1024) / (2 * 1024),
57183873Sraj		    sl->sl_ncylinders, sl->sl_acylinders,
58183873Sraj		    sl->sl_ntracks, sl->sl_nsectors);
59183873Sraj	}
60183873Sraj	sl->sl_interleave = 1;
61183873Sraj	sl->sl_sparespercyl = 0;
62183873Sraj	sl->sl_rpm = 3600;
63183873Sraj
64183873Sraj	for (c1 = c2->part; c1 != NULL; c1 = c1->next) {
65183873Sraj		p = c1->name;
66183873Sraj		p += strlen(p);
67183873Sraj		p--;
68183873Sraj		if (*p < 'a' || *p > 'h')
69191954Skuriyama			continue;
70191954Skuriyama		i = *p - 'a';
71183873Sraj		sl->sl_part[i].sdkp_cyloffset = c1->offset / secpercyl;
72183873Sraj		sl->sl_part[i].sdkp_nsectors = c1->size;
73183873Sraj	}
74183873Sraj
75183873Sraj	/*
76183873Sraj	 * We need to fill in the "RAW" partition as well.  Emperical data
77183873Sraj	 * seems to indicate that this covers the "obviously" visible part
78266328Sian	 * of the disk, ie: sl->sl_ncylinders.
79183873Sraj	 */
80183873Sraj	sl->sl_part[2].sdkp_cyloffset = 0;
81183873Sraj	sl->sl_part[2].sdkp_nsectors = sl->sl_ncylinders * secpercyl;
82183873Sraj
83183873Sraj	sp1 = (u_short *)sl;
84183873Sraj	sp2 = (u_short *)(sl + 1);
85194845Sraj	sl->sl_cksum = cksum = 0;
86194845Sraj	while (sp1 < sp2)
87220982Smav		cksum ^= *sp1++;
88209131Sraj	sl->sl_cksum = cksum;
89209131Sraj
90209131Sraj	strcpy(device,_PATH_DEV);
91209131Sraj        strcat(device,d1->name);
92
93        fd = open(device,O_RDWR);
94        if (fd < 0) {
95                warn("open(%s) failed", device);
96                return (1);
97        }
98
99	write_block(fd, 0, sl, sizeof *sl);
100
101	for (i = 1; i < 16; i++)
102		write_block(fd, i, d1->boot1 + (i * 512), 512);
103
104	close(fd);
105	return 0;
106}
107