* Implement POSIX (XSI)'s header file re_comp.h
[dragonfly.git] / release / write_mfs_in_kernel.c
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@login.dknet.dk> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * $FreeBSD: src/release/write_mfs_in_kernel.c,v 1.7 1999/12/16 02:14:30 jkh Exp $
10  * $DragonFly: src/release/Attic/write_mfs_in_kernel.c,v 1.2 2003/06/17 04:27:17 dillon Exp $
11  *
12  * This program patches a filesystem into a kernel made with MD_ROOT
13  * option.
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <sys/types.h>
21 #include <sys/param.h>
22 #include <sys/stat.h>
23 #include <ufs/ffs/fs.h>
24
25 static int force = 0;   /* don't check for zeros, may corrupt kernel */
26
27 int
28 main(int argc, char **argv)
29 {
30         unsigned char *buf_kernel, *buf_fs, *p,*q, *prog;
31         int fd_kernel, fd_fs, ch, errs=0;
32         struct stat st_kernel, st_fs;
33         u_long l;
34
35         prog= *argv;
36         while ((ch = getopt(argc, argv, "f")) != EOF)
37                 switch(ch) {                                                       
38                 case 'f':                                                          
39                         force = 1 - force;
40                         break;   
41                 default:
42                         errs++;
43                 }
44         argc -= optind;
45         argv += optind;
46
47         if (errs || argc != 2) {
48                 fprintf(stderr,"Usage:\n\t%s [-f] kernel fs\n", prog);
49                 exit(2);
50         }
51         --argv; /* original prog did not use getopt(3) */
52         fd_kernel = open(argv[1],O_RDWR);
53         if (fd_kernel < 0) { perror(argv[1]); exit(2); }
54         fstat(fd_kernel,&st_kernel);
55         fd_fs = open(argv[2],O_RDONLY);
56         if (fd_fs < 0) { perror(argv[2]); exit(2); }
57         fstat(fd_fs,&st_fs);
58         buf_kernel = malloc(st_kernel.st_size);
59         if (!buf_kernel) { perror("malloc"); exit(2); }
60         buf_fs = malloc(st_fs.st_size);
61         if (!buf_fs) { perror("malloc"); exit(2); }
62         if (st_kernel.st_size != read(fd_kernel,buf_kernel,st_kernel.st_size))
63                 { perror(argv[1]); exit(2); }
64         if (st_fs.st_size != read(fd_fs,buf_fs,st_fs.st_size))
65                 { perror(argv[2]); exit(2); }
66         for(l=0,p=buf_kernel; l < st_kernel.st_size - st_fs.st_size ; l++,p++ )
67                 if(*p == 'M' && !strcmp(p,"MFS Filesystem goes here"))
68                         goto found;
69         fprintf(stderr,"MFS filesystem signature not found in %s\n",argv[1]);
70         exit(1);
71 found:
72         if (!force)
73                 for(l=0,q= p + SBOFF; l < st_fs.st_size - SBOFF ; l++,q++ )
74                         if (*q)
75                                 goto fail;
76         memcpy(p+SBOFF,buf_fs+SBOFF,st_fs.st_size-SBOFF);
77         lseek(fd_kernel,0L,SEEK_SET);
78         if (st_kernel.st_size != write(fd_kernel,buf_kernel,st_kernel.st_size))
79                 { perror(argv[1]); exit(2); }
80         exit(0);
81 fail:
82         l += SBOFF;
83         fprintf(stderr,"Obstruction in kernel after %ld bytes (%ld Kbyte)\n",
84                 l, l/1024);
85         fprintf(stderr,"Filesystem is %ld bytes (%ld Kbyte)\n",
86                 (u_long)st_fs.st_size, (u_long)st_fs.st_size/1024);
87         exit(1);
88 }
89
90 /*
91  * I added a '-f' option to force writing the image into the kernel, even when
92  * there is already data (i.e. not zero) in the written area. This is useful
93  * to rewrite a changed MD-image. Beware: If the written image is larger than
94  * the space reserved in the kernel (with option MD_ROOT) then
95  * THIS WILL CORRUPT THE KERNEL!
96  *
97  */