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