Initial import from FreeBSD RELENG_4:
[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  *
11  * This program patches a filesystem into a kernel made with MD_ROOT
12  * option.
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <sys/types.h>
20 #include <sys/param.h>
21 #include <sys/stat.h>
22 #include <ufs/ffs/fs.h>
23
24 static int force = 0;   /* don't check for zeros, may corrupt kernel */
25
26 int
27 main(int argc, char **argv)
28 {
29         unsigned char *buf_kernel, *buf_fs, *p,*q, *prog;
30         int fd_kernel, fd_fs, ch, errs=0;
31         struct stat st_kernel, st_fs;
32         u_long l;
33
34         prog= *argv;
35         while ((ch = getopt(argc, argv, "f")) != EOF)
36                 switch(ch) {                                                       
37                 case 'f':                                                          
38                         force = 1 - force;
39                         break;   
40                 default:
41                         errs++;
42                 }
43         argc -= optind;
44         argv += optind;
45
46         if (errs || argc != 2) {
47                 fprintf(stderr,"Usage:\n\t%s [-f] kernel fs\n", prog);
48                 exit(2);
49         }
50         --argv; /* original prog did not use getopt(3) */
51         fd_kernel = open(argv[1],O_RDWR);
52         if (fd_kernel < 0) { perror(argv[1]); exit(2); }
53         fstat(fd_kernel,&st_kernel);
54         fd_fs = open(argv[2],O_RDONLY);
55         if (fd_fs < 0) { perror(argv[2]); exit(2); }
56         fstat(fd_fs,&st_fs);
57         buf_kernel = malloc(st_kernel.st_size);
58         if (!buf_kernel) { perror("malloc"); exit(2); }
59         buf_fs = malloc(st_fs.st_size);
60         if (!buf_fs) { perror("malloc"); exit(2); }
61         if (st_kernel.st_size != read(fd_kernel,buf_kernel,st_kernel.st_size))
62                 { perror(argv[1]); exit(2); }
63         if (st_fs.st_size != read(fd_fs,buf_fs,st_fs.st_size))
64                 { perror(argv[2]); exit(2); }
65         for(l=0,p=buf_kernel; l < st_kernel.st_size - st_fs.st_size ; l++,p++ )
66                 if(*p == 'M' && !strcmp(p,"MFS Filesystem goes here"))
67                         goto found;
68         fprintf(stderr,"MFS filesystem signature not found in %s\n",argv[1]);
69         exit(1);
70 found:
71         if (!force)
72                 for(l=0,q= p + SBOFF; l < st_fs.st_size - SBOFF ; l++,q++ )
73                         if (*q)
74                                 goto fail;
75         memcpy(p+SBOFF,buf_fs+SBOFF,st_fs.st_size-SBOFF);
76         lseek(fd_kernel,0L,SEEK_SET);
77         if (st_kernel.st_size != write(fd_kernel,buf_kernel,st_kernel.st_size))
78                 { perror(argv[1]); exit(2); }
79         exit(0);
80 fail:
81         l += SBOFF;
82         fprintf(stderr,"Obstruction in kernel after %ld bytes (%ld Kbyte)\n",
83                 l, l/1024);
84         fprintf(stderr,"Filesystem is %ld bytes (%ld Kbyte)\n",
85                 (u_long)st_fs.st_size, (u_long)st_fs.st_size/1024);
86         exit(1);
87 }
88
89 /*
90  * I added a '-f' option to force writing the image into the kernel, even when
91  * there is already data (i.e. not zero) in the written area. This is useful
92  * to rewrite a changed MD-image. Beware: If the written image is larger than
93  * the space reserved in the kernel (with option MD_ROOT) then
94  * THIS WILL CORRUPT THE KERNEL!
95  *
96  */