annotate bootmatch/comp_readbin.c @ 20:aa2fd60a44fb

new shell scripts c11x-gen-fc-{ffs-img,script}
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 10 Jun 2023 23:53:15 +0000
parents bfcc8180cf3c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * Bootmatch compiler: here we read the binary file.
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/file.h>
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/stat.h>
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdio.h>
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdlib.h>
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <unistd.h>
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include "comp_defs.h"
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 extern u_char boot_image[BOOT_BLOCK_SIZE];
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 void
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 read_bin_file(filename)
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 char *filename;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 {
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 int fd;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 struct stat st;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 fd = open(filename, O_RDONLY);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 if (fd < 0) {
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 perror(filename);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 exit(1);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 }
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 fstat(fd, &st);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 if (!S_ISREG(st.st_mode)) {
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 fprintf(stderr, "error: %s is not a regular file\n", filename);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 exit(1);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 }
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 if (st.st_size != BOOT_BLOCK_SIZE) {
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 fprintf(stderr, "error: %s has wrong length\n", filename);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 exit(1);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 }
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 read(fd, boot_image, BOOT_BLOCK_SIZE);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 close(fd);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }