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 * ----------------------------------------------------------------------------
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 $
12 * This program patches a filesystem into a kernel made with MD_ROOT
20 #include <sys/types.h>
21 #include <sys/param.h>
23 #include <ufs/ffs/fs.h>
25 static int force = 0; /* don't check for zeros, may corrupt kernel */
28 main(int argc, char **argv)
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;
36 while ((ch = getopt(argc, argv, "f")) != EOF)
47 if (errs || argc != 2) {
48 fprintf(stderr,"Usage:\n\t%s [-f] kernel fs\n", prog);
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); }
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"))
69 fprintf(stderr,"MFS filesystem signature not found in %s\n",argv[1]);
73 for(l=0,q= p + SBOFF; l < st_fs.st_size - SBOFF ; l++,q++ )
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); }
83 fprintf(stderr,"Obstruction in kernel after %ld bytes (%ld Kbyte)\n",
85 fprintf(stderr,"Filesystem is %ld bytes (%ld Kbyte)\n",
86 (u_long)st_fs.st_size, (u_long)st_fs.st_size/1024);
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!