License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-block.git] / arch / mn10300 / boot / tools / build.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright (C) 1991, 1992  Linus Torvalds
4  *  Copyright (C) 1997 Martin Mares
5  */
6
7 /*
8  * This file builds a disk-image from three different files:
9  *
10  * - bootsect: exactly 512 bytes of 8086 machine code, loads the rest
11  * - setup: 8086 machine code, sets up system parm
12  * - system: 80386 code for actual system
13  *
14  * It does some checking that all files are of the correct type, and
15  * just writes the result to stdout, removing headers and padding to
16  * the right amount. It also writes some system data to stderr.
17  */
18
19 /*
20  * Changes by tytso to allow root device specification
21  * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
22  * Cross compiling fixes by Gertjan van Wingerde, July 1996
23  * Rewritten by Martin Mares, April 1997
24  */
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/sysmacros.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <asm/boot.h>
36
37 #define DEFAULT_MAJOR_ROOT 0
38 #define DEFAULT_MINOR_ROOT 0
39
40 /* Minimal number of setup sectors (see also bootsect.S) */
41 #define SETUP_SECTS 4
42
43 uint8_t buf[1024];
44 int fd;
45 int is_big_kernel;
46
47 __attribute__((noreturn))
48 void die(const char *str, ...)
49 {
50         va_list args;
51         va_start(args, str);
52         vfprintf(stderr, str, args);
53         fputc('\n', stderr);
54         exit(1);
55 }
56
57 void file_open(const char *name)
58 {
59         fd = open(name, O_RDONLY, 0);
60         if (fd < 0)
61                 die("Unable to open `%s': %m", name);
62 }
63
64 __attribute__((noreturn))
65 void usage(void)
66 {
67         die("Usage: build [-b] bootsect setup system [rootdev] [> image]");
68 }
69
70 int main(int argc, char **argv)
71 {
72         unsigned int i, c, sz, setup_sectors;
73         uint32_t sys_size;
74         uint8_t major_root, minor_root;
75         struct stat sb;
76
77         if (argc > 2 && !strcmp(argv[1], "-b")) {
78                 is_big_kernel = 1;
79                 argc--, argv++;
80         }
81         if ((argc < 4) || (argc > 5))
82                 usage();
83         if (argc > 4) {
84                 if (!strcmp(argv[4], "CURRENT")) {
85                         if (stat("/", &sb)) {
86                                 perror("/");
87                                 die("Couldn't stat /");
88                         }
89                         major_root = major(sb.st_dev);
90                         minor_root = minor(sb.st_dev);
91                 } else if (strcmp(argv[4], "FLOPPY")) {
92                         if (stat(argv[4], &sb)) {
93                                 perror(argv[4]);
94                                 die("Couldn't stat root device.");
95                         }
96                         major_root = major(sb.st_rdev);
97                         minor_root = minor(sb.st_rdev);
98                 } else {
99                         major_root = 0;
100                         minor_root = 0;
101                 }
102         } else {
103                 major_root = DEFAULT_MAJOR_ROOT;
104                 minor_root = DEFAULT_MINOR_ROOT;
105         }
106         fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
107
108         file_open(argv[1]);
109         i = read(fd, buf, sizeof(buf));
110         fprintf(stderr, "Boot sector %d bytes.\n", i);
111         if (i != 512)
112                 die("Boot block must be exactly 512 bytes");
113         if (buf[510] != 0x55 || buf[511] != 0xaa)
114                 die("Boot block hasn't got boot flag (0xAA55)");
115         buf[508] = minor_root;
116         buf[509] = major_root;
117         if (write(1, buf, 512) != 512)
118                 die("Write call failed");
119         close(fd);
120
121         /* Copy the setup code */
122         file_open(argv[2]);
123         for (i = 0; (c = read(fd, buf, sizeof(buf))) > 0; i += c)
124                 if (write(1, buf, c) != c)
125                         die("Write call failed");
126         if (c != 0)
127                 die("read-error on `setup'");
128         close(fd);
129
130         /* Pad unused space with zeros */
131         setup_sectors = (i + 511) / 512;
132         /* for compatibility with ancient versions of LILO. */
133         if (setup_sectors < SETUP_SECTS)
134                 setup_sectors = SETUP_SECTS;
135         fprintf(stderr, "Setup is %d bytes.\n", i);
136         memset(buf, 0, sizeof(buf));
137         while (i < setup_sectors * 512) {
138                 c = setup_sectors * 512 - i;
139                 if (c > sizeof(buf))
140                         c = sizeof(buf);
141                 if (write(1, buf, c) != c)
142                         die("Write call failed");
143                 i += c;
144         }
145
146         file_open(argv[3]);
147         if (fstat(fd, &sb))
148                 die("Unable to stat `%s': %m", argv[3]);
149         sz = sb.st_size;
150         fprintf(stderr, "System is %d kB\n", sz / 1024);
151         sys_size = (sz + 15) / 16;
152         /* 0x28000*16 = 2.5 MB, conservative estimate for the current maximum */
153         if (sys_size > (is_big_kernel ? 0x28000 : DEF_SYSSIZE))
154                 die("System is too big. Try using %smodules.",
155                         is_big_kernel ? "" : "bzImage or ");
156         if (sys_size > 0xffff)
157                 fprintf(stderr,
158                         "warning: kernel is too big for standalone boot "
159                         "from floppy\n");
160         while (sz > 0) {
161                 int l, n;
162
163                 l = (sz > sizeof(buf)) ? sizeof(buf) : sz;
164                 n = read(fd, buf, l);
165                 if (n != l) {
166                         if (n < 0)
167                                 die("Error reading %s: %m", argv[3]);
168                         else
169                                 die("%s: Unexpected EOF", argv[3]);
170                 }
171                 if (write(1, buf, l) != l)
172                         die("Write failed");
173                 sz -= l;
174         }
175         close(fd);
176
177         /* Write sizes to the bootsector */
178         if (lseek(1, 497, SEEK_SET) != 497)
179                 die("Output: seek failed");
180         buf[0] = setup_sectors;
181         if (write(1, buf, 1) != 1)
182                 die("Write of setup sector count failed");
183         if (lseek(1, 500, SEEK_SET) != 500)
184                 die("Output: seek failed");
185         buf[0] = (sys_size & 0xff);
186         buf[1] = ((sys_size >> 8) & 0xff);
187         if (write(1, buf, 2) != 2)
188                 die("Write of image length failed");
189
190         return 0;
191 }