snippets: Introduce hello dir
[lcapit-junk-code.git] / dromfs / dromfs.c
bloba1535433a7f6be7ee4655667fed1926da82e96e6
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <sys/mman.h>
5 #include <fcntl.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <stdint.h>
10 #include <arpa/inet.h>
12 struct romfs_sb {
13 char name[9];
14 uint32_t size;
15 uint32_t checksum;
16 char vname[17];
19 static void usage(void)
21 printf("Usage: rromfs < device >\n");
24 static struct romfs_sb *get_romfs_sb(const char *path)
26 int fd;
27 uint8_t *p;
28 struct romfs_sb *sb;
29 const size_t sb_length = 16;
31 fd = open(path, O_RDONLY);
32 if (fd < 0) {
33 perror("open()");
34 exit(1);
37 sb = malloc(sizeof(struct romfs_sb));
38 if (!sb) {
39 perror("malloc()");
40 goto err_malloc;
42 memset(sb, 0, sizeof(struct romfs_sb));
44 p = mmap(NULL, sb_length, PROT_READ, MAP_PRIVATE, fd, (off_t) 0);
45 if (!p) {
46 perror("mmap()");
47 goto err_mmap;
50 /* File-system name */
51 memcpy(&sb->name, p, (size_t) 8);
53 /* Volume name */
54 memcpy(&sb->vname, (char *) p+16, (size_t) 16);
57 * FIXME: Is there a nicer way to read big
58 * endian values?
61 /* size */
62 memcpy(&sb->size, (uint8_t *) p+8, (size_t) 4);
63 sb->size = ntohl(sb->size);
65 /* checksum */
66 memcpy(&sb->checksum, (uint8_t *) p+12, (size_t) 4);
67 sb->checksum = ntohl(sb->checksum);
69 munmap(p, sb_length);
70 close(fd);
71 return sb;
73 err_mmap:
74 free(sb);
75 err_malloc:
76 close(fd);
77 exit(1);
80 int main(int argc, char *argv[])
82 struct romfs_sb *sb;
84 if (argc != 2) {
85 usage();
86 exit(1);
89 sb = get_romfs_sb(argv[1]);
90 if (!sb)
91 exit(1);
93 /* Super-block information */
94 printf("sig: %s\n", sb->name);
95 printf("size: %d\n", sb->size);
96 printf("checksum: %d\n", sb->checksum);
97 printf("Volume name: %s\n", sb->vname);
99 free(sb);
100 return 0;