| 1 | /* |
|---|
| 2 | * devinit.c |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2010 Sebastian Gottschall <gottschall@dd-wrt.com> |
|---|
| 5 | * |
|---|
| 6 | * This program is free software; you can redistribute it and/or |
|---|
| 7 | * modify it under the terms of the GNU General Public License |
|---|
| 8 | * as published by the Free Software Foundation; either version 2 |
|---|
| 9 | * of the License, or (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with this program; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 | * |
|---|
| 20 | * $Id: |
|---|
| 21 | */ |
|---|
| 22 | #include <stdio.h> |
|---|
| 23 | #include <stdlib.h> |
|---|
| 24 | #include <limits.h> |
|---|
| 25 | #include <time.h> |
|---|
| 26 | #include <unistd.h> |
|---|
| 27 | #include <errno.h> |
|---|
| 28 | #include <syslog.h> |
|---|
| 29 | #include <signal.h> |
|---|
| 30 | #include <string.h> |
|---|
| 31 | #include <termios.h> |
|---|
| 32 | #include <sys/klog.h> |
|---|
| 33 | #include <sys/types.h> |
|---|
| 34 | #include <sys/mount.h> |
|---|
| 35 | #include <sys/reboot.h> |
|---|
| 36 | #include <sys/stat.h> |
|---|
| 37 | #include <sys/sysmacros.h> |
|---|
| 38 | #include <sys/time.h> |
|---|
| 39 | #include <sys/utsname.h> |
|---|
| 40 | #include <sys/wait.h> |
|---|
| 41 | #include <sys/ioctl.h> |
|---|
| 42 | |
|---|
| 43 | #include <bcmnvram.h> |
|---|
| 44 | #include <shutils.h> |
|---|
| 45 | #include <utils.h> |
|---|
| 46 | #include <cymac.h> |
|---|
| 47 | #define SIOCGMIIREG 0x8948 /* Read MII PHY register. */ |
|---|
| 48 | #define SIOCSMIIREG 0x8949 /* Write MII PHY register. */ |
|---|
| 49 | #include <arpa/inet.h> |
|---|
| 50 | #include <sys/socket.h> |
|---|
| 51 | #include <linux/if.h> |
|---|
| 52 | #include <linux/sockios.h> |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | #ifdef HAVE_X86 |
|---|
| 56 | |
|---|
| 57 | static char *getdisc(void) // works only for squashfs |
|---|
| 58 | { |
|---|
| 59 | int i; |
|---|
| 60 | unsigned char *disks[]={"/dev/sda2","/dev/sdb2","/dev/sdc2","/dev/sdd2","/dev/sde2","/dev/sdf2","/dev/sdg2","/dev/sdh2","/dev/sdi2"}; |
|---|
| 61 | for (i = 0; i < 9; i++) { |
|---|
| 62 | char dev[64]; |
|---|
| 63 | |
|---|
| 64 | strcpy(dev, disks[i]); |
|---|
| 65 | FILE *in = fopen(dev, "rb"); |
|---|
| 66 | |
|---|
| 67 | if (in == NULL) |
|---|
| 68 | continue; // no second partition or disc does not |
|---|
| 69 | // exist, skipping |
|---|
| 70 | char buf[4]; |
|---|
| 71 | |
|---|
| 72 | fread(buf, 4, 1, in); |
|---|
| 73 | if (buf[0] == 'h' && buf[1] == 's' && buf[2] == 'q' |
|---|
| 74 | && buf[3] == 't') { |
|---|
| 75 | fclose(in); |
|---|
| 76 | // filesystem detected |
|---|
| 77 | return disks[i]; |
|---|
| 78 | } |
|---|
| 79 | fclose(in); |
|---|
| 80 | } |
|---|
| 81 | return NULL; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | #endif |
|---|
| 85 | void start_devinit(void) |
|---|
| 86 | { |
|---|
| 87 | unlink("/etc/nvram/.lock"); |
|---|
| 88 | cprintf("sysinit() proc\n"); |
|---|
| 89 | |
|---|
| 90 | /* |
|---|
| 91 | * /proc |
|---|
| 92 | */ |
|---|
| 93 | mount("proc", "/proc", "proc", MS_MGC_VAL, NULL); |
|---|
| 94 | mount("sysfs", "/sys", "sysfs", MS_MGC_VAL, NULL); |
|---|
| 95 | #ifdef HAVE_ATH9K |
|---|
| 96 | mount("debugfs", "/sys/kernel/debug", "debugfs", MS_MGC_VAL, NULL); |
|---|
| 97 | #endif |
|---|
| 98 | cprintf("sysinit() tmp\n"); |
|---|
| 99 | #ifdef HAVE_X86 |
|---|
| 100 | char dev[64]; |
|---|
| 101 | char *disc = getdisc(); |
|---|
| 102 | |
|---|
| 103 | if (disc == NULL) { |
|---|
| 104 | fprintf(stderr, |
|---|
| 105 | "no valid dd-wrt partition found, calling shell"); |
|---|
| 106 | eval("/bin/sh"); |
|---|
| 107 | exit(0); |
|---|
| 108 | } |
|---|
| 109 | // sprintf (dev, "/dev/discs/disc%d/part1", index); |
|---|
| 110 | // mount (dev, "/boot", "ext2", MS_MGC_VAL, NULL); |
|---|
| 111 | |
|---|
| 112 | sprintf(dev, "/dev/discs/disc%d/part3", index); |
|---|
| 113 | if (mount(dev, "/usr/local", "ext2", MS_MGC_VAL, NULL)) { |
|---|
| 114 | eval("/sbin/mke2fs", "-F", "-b", "1024", dev); |
|---|
| 115 | mount(dev, "/usr/local", "ext2", MS_MGC_VAL, NULL); |
|---|
| 116 | // eval("/bin/tar", "-xvvjf", "/etc/local.tar.bz2", "-C", "/"); |
|---|
| 117 | } |
|---|
| 118 | eval("mkdir", "-p", "/usr/local/nvram"); |
|---|
| 119 | #endif |
|---|
| 120 | /* |
|---|
| 121 | * /tmp |
|---|
| 122 | */ |
|---|
| 123 | mount("ramfs", "/tmp", "ramfs", MS_MGC_VAL, NULL); |
|---|
| 124 | #ifdef HAVE_HOTPLUG2 |
|---|
| 125 | // shell-skript. otherwise we loose our console |
|---|
| 126 | system("echo >/proc/sys/kernel/hotplug"); |
|---|
| 127 | system("mount -t tmpfs none /dev -o size=512K"); |
|---|
| 128 | system("mknod /dev/console c 5 1"); |
|---|
| 129 | system("mknod /dev/null c 1 3"); |
|---|
| 130 | system("mkdir /dev/pts"); |
|---|
| 131 | #else |
|---|
| 132 | // fix for linux kernel 2.6 |
|---|
| 133 | eval("mknod", "/dev/ppp", "c", "108", "0"); |
|---|
| 134 | #endif |
|---|
| 135 | // fix me udevtrigger does not create that (yet) not registered? |
|---|
| 136 | eval("mknod", "/dev/nvram", "c", "229", "0"); |
|---|
| 137 | eval("mknod", "/dev/watchdog", "c", "10", "130"); |
|---|
| 138 | |
|---|
| 139 | mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL, NULL); |
|---|
| 140 | mount("devpts", "/proc/bus/usb", "usbfs", MS_MGC_VAL, NULL); |
|---|
| 141 | eval("mkdir", "/tmp/www"); |
|---|
| 142 | |
|---|
| 143 | unlink("/tmp/nvram/.lock"); |
|---|
| 144 | eval("mkdir", "/tmp/nvram"); |
|---|
| 145 | |
|---|
| 146 | /* |
|---|
| 147 | * /var |
|---|
| 148 | */ |
|---|
| 149 | mkdir("/tmp/var", 0777); |
|---|
| 150 | mkdir("/var/lock", 0777); |
|---|
| 151 | mkdir("/var/lock/subsys", 0777); |
|---|
| 152 | mkdir("/var/log", 0777); |
|---|
| 153 | mkdir("/var/run", 0777); |
|---|
| 154 | mkdir("/var/tmp", 0777); |
|---|
| 155 | fprintf(stderr,"starting hotplug\n"); |
|---|
| 156 | #ifdef HAVE_HOTPLUG2 |
|---|
| 157 | system("/etc/hotplug2.startup"); |
|---|
| 158 | #endif |
|---|
| 159 | fprintf(stderr,"done\n"); |
|---|
| 160 | } |
|---|