static int __init mount_nfs_root(void) { char *root_dev, *root_data; unsigned int timeout; int try, err; err = nfs_root_data(&root_dev, &root_data); if (err != 0) return 0; /* * The server or network may not be ready, so try several * times. Stop after a few tries in case the client wants * to fall back to other boot methods. */ timeout = NFSROOT_TIMEOUT_MIN; for (try = 1; ; try++) { err = do_mount_root(root_dev, "nfs", root_mountflags, root_data); if (err == 0) return 1; if (try > NFSROOT_RETRY_MAX) break; /* Wait, in case the server refused us immediately */ ssleep(timeout); timeout <<= 1; if (timeout > NFSROOT_TIMEOUT_MAX) timeout = NFSROOT_TIMEOUT_MAX; } return 0; } #endif #if defined(CONFIG_BLK_DEV_RAM) || defined(CONFIG_BLK_DEV_FD) void __init change_floppy(char *fmt, ...) { struct termios termios; char buf[80]; char c; int fd; va_list args; va_start(args, fmt); vsprintf(buf, fmt, args); va_end(args); fd = sys_open("/dev/root", O_RDWR | O_NDELAY, 0); if (fd >= 0) { sys_ioctl(fd, FDEJECT, 0); sys_close(fd); } printk(KERN_NOTICE "VFS: Insert %s and press ENTER\n", buf); fd = sys_open("/dev/console", O_RDWR, 0); if (fd >= 0) { sys_ioctl(fd, TCGETS, (long)&termios); termios.c_lflag &= ~ICANON; sys_ioctl(fd, TCSETSF, (long)&termios); sys_read(fd, &c, 1); termios.c_lflag |= ICANON; sys_ioctl(fd, TCSETSF, (long)&termios); sys_close(fd); } } #endif void __init mount_root(void) { #ifdef CONFIG_ROOT_NFS if (ROOT_DEV == Root_NFS) { if (mount_nfs_root()) return; printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n"); ROOT_DEV = Root_FD0; } #endif #ifdef CONFIG_BLK_DEV_FD if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) { /* rd_doload is 2 for a dual initrd/ramload setup */ if (rd_doload==2) { if (rd_load_disk(1)) { ROOT_DEV = Root_RAM1; root_device_name = NULL; } } else change_floppy("root floppy"); } #endif #ifdef CONFIG_BLOCK { int err = create_dev("/dev/root", ROOT_DEV); if (err < 0) pr_emerg("Failed to create /dev/root: %d\n", err); mount_block_root("/dev/root", root_mountflags); } #endif } /* * Prepare the namespace - decide what/where to mount, load ramdisks, etc. */ void __init prepare_namespace(void) { int is_floppy; if (root_delay) { printk(KERN_INFO "Waiting %d sec before mounting root device...\n", root_delay); ssleep(root_delay); } /* * wait for the known devices to complete their probing * * Note: this is a potential source of long boot delays. * For example, it is not atypical to wait 5 seconds here * for the touchpad of a laptop to initialize. */ wait_for_device_probe(); md_run_setup(); if (saved_root_name[0]) { root_device_name = saved_root_name; if (!strncmp(root_device_name, "mtd", 3) || !strncmp(root_device_name, "ubi", 3)) { mount_block_root(root_device_name, root_mountflags); goto out; } ROOT_DEV = name_to_dev_t(root_device_name); if (strncmp(root_device_name, "/dev/", 5) == 0) root_device_name += 5; } if (initrd_load()) goto out; /* wait for any asynchronous scanning to complete */ if ((ROOT_DEV == 0) && root_wait) { printk(KERN_INFO "Waiting for root device %s...\n", saved_root_name); while (driver_probe_done() != 0 || (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0) msleep(5); async_synchronize_full(); } is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR; if (is_floppy && rd_doload && rd_load_disk(0)) ROOT_DEV = Root_RAM0; mount_root(); out: devtmpfs_mount("dev"); sys_mount(".", "/", NULL, MS_MOVE, NULL); sys_chroot("."); }
/* This may be used only once, enforced by 'static int callable' */ int sys_setup(void * BIOS) { static int callable = 1; int i,drive; unsigned char cmos_disks; struct partition *p; struct buffer_head * bh; if (!callable) return -1; callable = 0; #ifndef HD_TYPE for (drive=0 ; drive<2 ; drive++) { hd_info[drive].cyl = *(unsigned short *) BIOS; hd_info[drive].head = *(unsigned char *) (2+BIOS); hd_info[drive].wpcom = *(unsigned short *) (5+BIOS); hd_info[drive].ctl = *(unsigned char *) (8+BIOS); hd_info[drive].lzone = *(unsigned short *) (12+BIOS); hd_info[drive].sect = *(unsigned char *) (14+BIOS); BIOS += 16; } if (hd_info[1].cyl) NR_HD=2; else NR_HD=1; #endif for (i=0 ; i<NR_HD ; i++) { hd[i*5].start_sect = 0; hd[i*5].nr_sects = hd_info[i].head* hd_info[i].sect*hd_info[i].cyl; } /* We querry CMOS about hard disks : it could be that we have a SCSI/ESDI/etc controller that is BIOS compatable with ST-506, and thus showing up in our BIOS table, but not register compatable, and therefore not present in CMOS. Furthurmore, we will assume that our ST-506 drives <if any> are the primary drives in the system, and the ones reflected as drive 1 or 2. The first drive is stored in the high nibble of CMOS byte 0x12, the second in the low nibble. This will be either a 4 bit drive type or 0xf indicating use byte 0x19 for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS. Needless to say, a non-zero value means we have an AT controller hard disk for that drive. */ if ((cmos_disks = CMOS_READ(0x12)) & 0xf0) if (cmos_disks & 0x0f) NR_HD = 2; else NR_HD = 1; else NR_HD = 0; for (i = NR_HD ; i < 2 ; i++) { hd[i*5].start_sect = 0; hd[i*5].nr_sects = 0; } for (drive=0 ; drive<NR_HD ; drive++) { if (!(bh = bread(0x300 + drive*5,0))) { printk("Unable to read partition table of drive %d\n\r", drive); panic(""); } if (bh->b_data[510] != 0x55 || (unsigned char) bh->b_data[511] != 0xAA) { printk("Bad partition table on drive %d\n\r",drive); panic(""); } p = 0x1BE + (void *)bh->b_data; for (i=1;i<5;i++,p++) { hd[i+5*drive].start_sect = p->start_sect; hd[i+5*drive].nr_sects = p->nr_sects; } brelse(bh); } if (NR_HD) printk("Partition table%s ok.\n\r",(NR_HD>1)?"s":""); rd_load(); mount_root(); return (0); }
int __init change_root(kdev_t new_root_dev,const char *put_old) { kdev_t old_root_dev; struct vfsmount *vfsmnt; struct dentry *old_root,*old_pwd,*dir_d = NULL; int error; old_root = current->fs->root; old_pwd = current->fs->pwd; old_root_dev = ROOT_DEV; if (!fs_may_mount(new_root_dev)) { printk(KERN_CRIT "New root is busy. Staying in initrd.\n"); return -EBUSY; } ROOT_DEV = new_root_dev; mount_root(); dput(old_root); dput(old_pwd); #if 1 shrink_dcache(); printk("change_root: old root has d_count=%d\n", old_root->d_count); #endif /* * Get the new mount directory */ dir_d = lookup_dentry(put_old, NULL, 1); if (IS_ERR(dir_d)) { error = PTR_ERR(dir_d); } else if (!dir_d->d_inode) { dput(dir_d); error = -ENOENT; } else { error = 0; } if (!error && dir_d->d_covers != dir_d) { dput(dir_d); error = -EBUSY; } if (!error && !S_ISDIR(dir_d->d_inode->i_mode)) { dput(dir_d); error = -ENOTDIR; } if (error) { int umount_error; printk(KERN_NOTICE "Trying to unmount old root ... "); umount_error = do_umount(old_root_dev,1, 0); if (!umount_error) { printk("okay\n"); /* special: the old device driver is going to be a ramdisk and the point of this call is to free its protected memory (even if dirty). */ destroy_buffers(old_root_dev); return 0; } printk(KERN_ERR "error %d\n",umount_error); return error; } remove_vfsmnt(old_root_dev); vfsmnt = add_vfsmnt(old_root->d_sb, "/dev/root.old", put_old); if (vfsmnt) { d_mount(dir_d,old_root); return 0; } printk(KERN_CRIT "Trouble: add_vfsmnt failed\n"); return -ENOMEM; }
static void __init handle_initrd(void) { int error; int pid; real_root_dev = new_encode_dev(ROOT_DEV); create_dev("/dev/root.old", Root_RAM0); /* mount initrd on rootfs' /root */ mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY); sys_mkdir((const char __force_user *)"/old", 0700); root_fd = sys_open((const char __force_user *)"/", 0, 0); old_fd = sys_open((const char __force_user *)"/old", 0, 0); /* move initrd over / and chdir/chroot in initrd root */ sys_chdir((const char __force_user *)"/root"); sys_mount((char __force_user *)".", (char __force_user *)"/", NULL, MS_MOVE, NULL); sys_chroot((const char __force_user *)"."); /* * In case that a resume from disk is carried out by linuxrc or one of * its children, we need to tell the freezer not to wait for us. */ current->flags |= PF_FREEZER_SKIP; pid = kernel_thread(do_linuxrc, "/linuxrc", SIGCHLD); if (pid > 0) while (pid != sys_wait4(-1, NULL, 0, NULL)) yield(); current->flags &= ~PF_FREEZER_SKIP; /* move initrd to rootfs' /old */ sys_fchdir(old_fd); sys_mount((char __force_user *)"/", (char __force_user *)".", NULL, MS_MOVE, NULL); /* switch root and cwd back to / of rootfs */ sys_fchdir(root_fd); sys_chroot((const char __force_user *)"."); sys_close(old_fd); sys_close(root_fd); if (new_decode_dev(real_root_dev) == Root_RAM0) { sys_chdir((const char __force_user *)"/old"); return; } ROOT_DEV = new_decode_dev(real_root_dev); mount_root(); printk(KERN_NOTICE "Trying to move old root to /initrd ... "); error = sys_mount((char __force_user *)"/old", (char __force_user *)"/root/initrd", NULL, MS_MOVE, NULL); if (!error) printk("okay\n"); else { int fd = sys_open((const char __force_user *)"/dev/root.old", O_RDWR, 0); if (error == -ENOENT) printk("/initrd does not exist. Ignored.\n"); else printk("failed\n"); printk(KERN_NOTICE "Unmounting old root\n"); sys_umount((char __force_user *)"/old", MNT_DETACH); printk(KERN_NOTICE "Trying to free ramdisk memory ... "); if (fd < 0) { error = fd; } else { error = sys_ioctl(fd, BLKFLSBUF, 0); sys_close(fd); } printk(!error ? "okay\n" : "failed\n"); } }
int main(int argc, char * argv[], char * env[]) { char line[128], command[128], pathname[128]; int ID; // DEVICE SELECT get_device(); // INITIALIZE init(); // MOUNT ROOT mount_root(); // PROCESS LOOP while(1) { strcpy(line, ""); strcpy(command, ""); strcpy(pathname, ""); strcpy(completePath, ""); printf("\n\ninput a command (type help for more info): "); //read a line containting command [pathname]; // [ ] means optional fgets(line, 256, stdin); line[strlen(line)-1] = '\0'; //Find the command string and call the corresponding function; parseString(line, arg1, command, pathname); compPath(pathname); printf("PATHNAME: %s\n", pathname); ID = findCommand(command); switch(ID) { case -1 : printDir(running->cwd->ino); break; case 0 : _menu (arg1, pathname); break; case 1 : _ls (arg1, pathname); break; case 2 : _cd (arg1, pathname); break; case 3 : _mkdir (arg1, pathname); break; case 4 : _rmdir (arg1, pathname); break; case 5 : _pwd (arg1, pathname); break; case 6 : _creat0(arg1, pathname); break; case 7 : _rm (arg1, pathname); break; case 8 : _stat (arg1, pathname); break; case 9 : compPath(arg1); _link(arg1, pathname); break; case 10: _unlink(arg1, pathname); break; case 11: compPath(arg1); _symlink(arg1, pathname); break; case 12: _touch (arg1, pathname); break; case 13: _chmod (arg1, pathname); break; case 14: _chown (arg1, pathname); break; case 15: _chgrp (arg1, pathname); break; case 16: _open (arg1, pathname); break; case 17: _close (arg1, pathname); break; case 18: _read (arg1, pathname); break; case 19: _write (arg1, pathname); break; case 20: _pfd (arg1, pathname); break; case 21: _lseek (arg1, pathname); break; case 22: _cat (arg1, pathname); break; case 23: _cp (arg1, pathname); break; case 24: _mv (arg1, pathname); break; case 25: __exit (arg1, pathname); break; } } quit(); return 0; }