1995-04-25 03:45:18 +00:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
|
|
|
* <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
|
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*
|
1999-08-28 01:35:59 +00:00
|
|
|
* $FreeBSD$
|
1995-04-25 03:45:18 +00:00
|
|
|
*
|
1999-12-16 02:08:52 +00:00
|
|
|
* This program patches a filesystem into a kernel made with MD_ROOT
|
1995-04-25 03:45:18 +00:00
|
|
|
* option.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <ufs/ffs/fs.h>
|
|
|
|
|
1997-09-18 18:27:34 +00:00
|
|
|
static int force = 0; /* don't check for zeros, may corrupt kernel */
|
|
|
|
|
|
|
|
int
|
1995-04-25 03:45:18 +00:00
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
1997-09-18 18:27:34 +00:00
|
|
|
unsigned char *buf_kernel, *buf_fs, *p,*q, *prog;
|
|
|
|
int fd_kernel, fd_fs, ch, errs=0;
|
1995-04-25 03:45:18 +00:00
|
|
|
struct stat st_kernel, st_fs;
|
|
|
|
u_long l;
|
|
|
|
|
1997-09-18 18:27:34 +00:00
|
|
|
prog= *argv;
|
|
|
|
while ((ch = getopt(argc, argv, "f")) != EOF)
|
|
|
|
switch(ch) {
|
|
|
|
case 'f':
|
|
|
|
force = 1 - force;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errs++;
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (errs || argc != 2) {
|
|
|
|
fprintf(stderr,"Usage:\n\t%s [-f] kernel fs\n", prog);
|
1995-04-25 03:45:18 +00:00
|
|
|
exit(2);
|
|
|
|
}
|
1997-09-18 18:27:34 +00:00
|
|
|
--argv; /* original prog did not use getopt(3) */
|
1995-04-25 03:45:18 +00:00
|
|
|
fd_kernel = open(argv[1],O_RDWR);
|
|
|
|
if (fd_kernel < 0) { perror(argv[1]); exit(2); }
|
|
|
|
fstat(fd_kernel,&st_kernel);
|
|
|
|
fd_fs = open(argv[2],O_RDONLY);
|
|
|
|
if (fd_fs < 0) { perror(argv[2]); exit(2); }
|
|
|
|
fstat(fd_fs,&st_fs);
|
|
|
|
buf_kernel = malloc(st_kernel.st_size);
|
|
|
|
if (!buf_kernel) { perror("malloc"); exit(2); }
|
|
|
|
buf_fs = malloc(st_fs.st_size);
|
|
|
|
if (!buf_fs) { perror("malloc"); exit(2); }
|
|
|
|
if (st_kernel.st_size != read(fd_kernel,buf_kernel,st_kernel.st_size))
|
|
|
|
{ perror(argv[1]); exit(2); }
|
|
|
|
if (st_fs.st_size != read(fd_fs,buf_fs,st_fs.st_size))
|
|
|
|
{ perror(argv[2]); exit(2); }
|
|
|
|
for(l=0,p=buf_kernel; l < st_kernel.st_size - st_fs.st_size ; l++,p++ )
|
1999-12-16 02:08:52 +00:00
|
|
|
if(*p == 'M' && !strcmp(p,"MD Filesystem goes here"))
|
1995-04-25 03:45:18 +00:00
|
|
|
goto found;
|
1999-12-16 02:08:52 +00:00
|
|
|
fprintf(stderr,"MD filesystem signature not found in %s\n",argv[1]);
|
1995-04-25 03:45:18 +00:00
|
|
|
exit(1);
|
1997-09-18 18:27:34 +00:00
|
|
|
found:
|
|
|
|
if (!force)
|
|
|
|
for(l=0,q= p + SBOFF; l < st_fs.st_size - SBOFF ; l++,q++ )
|
|
|
|
if (*q)
|
|
|
|
goto fail;
|
1995-04-25 03:45:18 +00:00
|
|
|
memcpy(p+SBOFF,buf_fs+SBOFF,st_fs.st_size-SBOFF);
|
|
|
|
lseek(fd_kernel,0L,SEEK_SET);
|
|
|
|
if (st_kernel.st_size != write(fd_kernel,buf_kernel,st_kernel.st_size))
|
|
|
|
{ perror(argv[1]); exit(2); }
|
|
|
|
exit(0);
|
1997-09-18 18:27:34 +00:00
|
|
|
fail:
|
1995-04-25 03:45:18 +00:00
|
|
|
l += SBOFF;
|
|
|
|
fprintf(stderr,"Obstruction in kernel after %ld bytes (%ld Kbyte)\n",
|
|
|
|
l, l/1024);
|
|
|
|
fprintf(stderr,"Filesystem is %ld bytes (%ld Kbyte)\n",
|
|
|
|
(u_long)st_fs.st_size, (u_long)st_fs.st_size/1024);
|
|
|
|
exit(1);
|
|
|
|
}
|
1997-09-18 18:27:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* I added a '-f' option to force writing the image into the kernel, even when
|
|
|
|
* there is already data (i.e. not zero) in the written area. This is useful
|
1999-12-16 02:08:52 +00:00
|
|
|
* to rewrite a changed MD-image. Beware: If the written image is larger than
|
|
|
|
* the space reserved in the kernel (with option MD_ROOT) then
|
1997-09-18 18:27:34 +00:00
|
|
|
* THIS WILL CORRUPT THE KERNEL!
|
|
|
|
*
|
|
|
|
*/
|