revert between 56095 -> 55830 in arch
[AROS.git] / arch / m68k-amiga / boot / floppy / install.c
blob9dfa647f5fe66cb22fd177355174b8e250b37aa8
1 /*
2 Copyright © 1995-2010, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <errno.h>
7 #include <string.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <fcntl.h>
14 #include <sys/stat.h>
16 typedef uint8_t UBYTE;
17 typedef uint32_t ULONG;
19 struct BootBlock {
20 UBYTE DiskType[4];
21 ULONG Chksum;
22 ULONG RootBlock;
23 UBYTE BootBlockCode[0];
24 } __attribute__((packed));
26 const UBYTE m68k_boot[] = {
27 0x43, 0xfa, 0x00, 0x18, 0x4e, 0xae, 0xff, 0xa0,
28 0x4a, 0x80, 0x67, 0x0a, 0x20, 0x40, 0x20, 0x68,
29 0x00, 0x16, 0x70, 0x00, 0x4e, 0x75, 0x70, 0xff,
30 0x60, 0xfa, 0x64, 0x6f, 0x73, 0x2e, 0x6c, 0x69,
31 0x62, 0x72, 0x61, 0x72, 0x79, 0x00 };
33 #define BOOTBLOCKSIZE 1024
35 /* Adapted from rom/boot/strap.c */
36 static void BootBlockChecksum(UBYTE *bootblock, size_t size)
38 ULONG crc = 0;
39 int i;
41 memset(bootblock+4, 0, 4);
43 for (i = 0; i < size; i += 4) {
44 ULONG v = (bootblock[i] << 24) | (bootblock[i + 1] << 16) |
45 (bootblock[i + 2] << 8) | bootblock[i + 3];
46 if (crc + v < crc)
47 crc++;
48 crc += v;
50 crc ^= 0xffffffff;
52 bootblock[4+0] = (crc >> 24) & 0xff;
53 bootblock[4+1] = (crc >> 16) & 0xff;
54 bootblock[4+2] = (crc >> 8) & 0xff;
55 bootblock[4+3] = (crc >> 0) & 0xff;
59 int main(int argc, char **argv)
61 int fd, err, i;
62 const char *image = argv[1];
63 UBYTE buff[BOOTBLOCKSIZE];
64 struct BootBlock *bootblock = (void *)&buff[0];
65 ULONG csum, psum;
67 fd = open(image, O_RDWR);
68 if (fd < 0) {
69 perror(image);
70 return EXIT_FAILURE;
73 err = read(fd, buff, sizeof(buff));
74 if (err < 0) {
75 perror(image);
76 close(fd);
79 lseek(fd, 0, SEEK_SET);
81 if (memcmp(bootblock->DiskType, "DOS", 3) != 0) {
82 fprintf(stderr, "%s: Not a DOS (OFS or FFS) disk\n", image);
83 close(fd);
84 return EXIT_FAILURE;
87 memset(bootblock->BootBlockCode, 0, sizeof(buff)-sizeof(struct BootBlock));
89 memcpy(bootblock->BootBlockCode, m68k_boot, sizeof(m68k_boot));
91 BootBlockChecksum(buff, sizeof(buff));
93 err = write(fd, buff, 1024);
94 if (err < 0) {
95 perror(image);
96 close(fd);
97 return EXIT_FAILURE;
100 close(fd);
101 return EXIT_SUCCESS;