summaryrefslogtreecommitdiff
path: root/package/aboot/src/srmbootfat/srmbootraw.c
blob: 3bba2d577ccd62af3ceaa2f8d1e70bc07956ef03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 *  SRMbootraw - SRM boot block composer for raw boot partitions.
 *  Copyright (C) 1998 Nikita Schmidt  <cetus@snowball.ucd.ie>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>

/* We assume little endiannes and possibly other Alpha properties as well. */
#ifndef __alpha__
#error This stuff must be compiled for Alpha.
#endif

#define BUFSIZE	65536

union bootsector {
	struct {
		unsigned char textlabel[64];
		unsigned char disklabel[276];
		unsigned char unused[140];
		u_int64_t count, start, flags;
		u_int64_t checksum;
	} srm;
	struct {
		u_int64_t contents[63];
		u_int64_t checksum;
	} check;
};


int main (int argc, char *argv[])
{
	int device, image;
	int i;
	union bootsector boot;
	char *buf;
	unsigned long size;
	ssize_t len;
	u_int64_t checksum;

	if (argc != 3)
		return printf ("Usage:  srmbootraw <boot device> <boot image>\n"), 1;

	if ((device = open (argv[1], O_RDWR)) < 0)
		return perror (argv[1]), 2;
	if ((image = open (argv[2], O_RDONLY)) < 0)
		return perror (argv[2]), 2;

	/* Read in the old bootsector */
	if (read (device, &boot, sizeof boot) != sizeof boot)
		return fprintf (stderr, "Can't read boot sector from %s\n", argv[1]), 2;

	if (!(buf = malloc (BUFSIZE)))
		return fprintf (stderr, "Can't allocate memory for %s\n", argv[2]), 2;

	/* Copy image onto the device */
	size = 0;
	while ((len = read (image, buf, BUFSIZE)) > 0)
	{
		if (write (device, buf, len) != len)
			return fprintf (stderr, "Can't write to %s\n", argv[1]), 2;
		size += len;
	}
	close (image);
	if (len == -1)
		return perror (argv[2]), 2;

	/* Fill in the bootstrap information */
	boot.srm.start = 1;
	boot.srm.count = (size + 511) / 512;	/* Convert to sectors */
	boot.srm.flags = 0;

	/* Put the checksum and write the boot sector. */
	checksum = 0;
	for (i = 0; i < 63; i++)
		checksum += boot.check.contents[i];
	boot.check.checksum = checksum;

	printf ("Writing SRM boot block: starting sector %u, block count %u\n",
		(unsigned)boot.srm.start, (unsigned)boot.srm.count);

	if (lseek (device, 0, SEEK_SET) == -1
	 || write (device, &boot, sizeof boot) != sizeof boot)
		return perror (argv[2]), 2;

	close (device);
	return 0;
}