home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 December / CHIPNET Aralık 1997.iso / linux / redhat / misc / src / install / install.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-11  |  8.4 KB  |  388 lines

  1. /*
  2.  * install.c
  3.  * 
  4.  * This is the first half of the install. It does just enough to get the
  5.  * second half going. It, and everything it needs, has to fit on one floppy
  6.  * along with a kernel and modules. Needless to say, it's a bit tight.
  7.  *
  8.  * Erik Troan (ewt@redhat.com)
  9.  *
  10.  * Copyright 1997 Red Hat Software 
  11.  *
  12.  * This software may be freely redistributed under the terms of the GNU
  13.  * public license.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  */
  20.  
  21. #include <alloca.h>
  22. #include <dirent.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <stdarg.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/stat.h>
  30. #include <sys/sysmacros.h>
  31. #include <sys/wait.h>
  32. #include <unistd.h>
  33. #include <zlib.h>
  34.  
  35. #include "devices.h"
  36. #include "fs.h"
  37. #include "install.h"
  38. #include "kbd.h"
  39. #include "log.h"
  40. #include "methods.h"
  41. #include "mono.h"
  42. #include "net.h"
  43. #include "newt.h"
  44. #include "perror.h"
  45. #include "run.h"
  46. #include "windows.h"
  47.  
  48. int testing = 0;
  49. int hackdisk = 0;
  50. int debug = 1;
  51.  
  52. char * welcomeText =
  53.     "Welcome to Red Hat Linux!\n"
  54.     "\n"
  55.     "This installation process is outlined in detail in the Official Red "
  56.     "Hat Linux User's Guide available from Red Hat Software. If you have "
  57.     "access to this manual, you should read the installation section before "
  58.     "continuing.\n\n"
  59.     "If you have purchased Official Red Hat Linux, be sure to register your "
  60.     "purchase through our web site, http://www.redhat.com."
  61.     ;
  62.  
  63. char * hackdiskText = "Would you like to use a hack disk now?";
  64.  
  65. void welcome(void) {
  66.     if (!testing)
  67.     winMessage(7, 4, 65, 16, "Red Hat Linux", welcomeText);
  68. }
  69.  
  70. void mountFloppy() {
  71.     if (!testing) {
  72.     logMessage("mounting ext2 fs on floppy");
  73.     doMount("fd0", "/tmp/bootdisk", "ext2", 1, 0);
  74.     logMessage("floppy filesystem mounted on /tmp/bootdisk");
  75.     }
  76. }
  77.  
  78. void showmtab(void) {
  79.     char buf[5000];
  80.     int fd, i;
  81.  
  82.     fd = open("/proc/mounts", O_RDONLY);
  83.     if (fd < 0) {
  84.     messageWindow("error", perrorstr("open /proc/mounts"));
  85.     return;
  86.     }
  87.  
  88.     i = read(fd, buf, sizeof(buf) - 1);
  89.     if (i < 1) {
  90.     close(fd);
  91.     messageWindow("error", perrorstr("read /proc/mounts"));
  92.     return;
  93.     }
  94.     close(fd);
  95.  
  96.     buf[i] = '\0';
  97.  
  98.     messageWindow("/proc/mounts", buf);
  99. }
  100.  
  101. void mountHackdisk() {
  102.     if (hackdisk) {
  103.     messageWindow("Hackdisk", "Insert hack disk");
  104.  
  105.     if (!testing) {
  106.         doMount("fd0", "/tmp/hackdisk", "ext2", 1, 0);
  107.  
  108.         symlink("/tmp/hackdisk/lib", "/lib");
  109.         symlink("/tmp/hackdisk/etc", "/etc");
  110.         /*showdir("/lib");
  111.         showdir("/tmp/hackdisk/bin");*/
  112.  
  113.         logMessage("hackdisk filesystem mounted on /tmp/bootdisk");
  114.         if (!fork()) {
  115.         execl("/tmp/hackdisk/bin/open", "open", "/tmp/hackdisk/bin/sh", NULL);
  116.         logMessage(perrorstr("exec of sh failed"));
  117.         }
  118.     }
  119.     }
  120. }
  121.  
  122. static int pcmciaChoicePanel(int * havePCMCIA) {
  123.     newtComponent label, f, answer;
  124.     newtComponent yes, no;
  125.  
  126.     #if defined(__sparc__) || defined(__alpha__)
  127.     *havePCMCIA = 0;
  128.     return 0;
  129.     #endif
  130.  
  131.     newtOpenWindow(21, 7, 35, 9, "PCMCIA Support");
  132.  
  133.     label = newtLabel(2, 2, "Do you need PCMCIA support?");
  134.  
  135.     yes = newtButton(5, 5, "Yes");
  136.     no = newtButton(22, 5, "No");
  137.  
  138.     f = newtForm(NULL, NULL, 0);
  139.     newtFormAddComponents(f, label, yes, no, NULL);
  140.     newtFormSetCurrent(f, no);
  141.     
  142.     answer = newtRunForm(f);
  143.     if (answer == f)
  144.     answer = newtFormGetCurrent(f);
  145.  
  146.     newtFormDestroy(f);
  147.     newtPopWindow();
  148.  
  149.     if (answer == no) 
  150.     *havePCMCIA = 0;
  151.     else
  152.     *havePCMCIA = 1;
  153.  
  154.     return 0;
  155. }
  156.  
  157. int setupPCMCIA(char ** arg) {
  158.     int rc, doit;
  159.     struct driversLoaded * dl = NULL;
  160.     int status;
  161.     char * args[] = { "/sbin/probe", NULL };
  162.     char * probeOutput;
  163.     static char pcic[20];
  164.  
  165.     pcmciaChoicePanel(&doit);
  166.     if (!doit) return 0;
  167.  
  168.     if ((rc = floppyRoot(NULL))) return rc;
  169.  
  170.     if (testing) {
  171.     sleep(2);
  172.     return 0;
  173.     }
  174.  
  175.     winStatus(35, 3, "PCMCIA", "Loading PCMCIA support...");
  176.  
  177.     chdir("/modules");
  178.     unlink("53c7,8xx.o.gz");
  179.     unlink("3c59x.o.gz");
  180.     unlink("cdrom.o.gz");
  181.     unlink("aztcd.o.gz");
  182.     unlink("cdu31a.o.gz");
  183.     unlink("cm206.o.gz");
  184.     unlink("de4x5.o.gz");
  185.     unlink("sbpcd.o.gz");
  186.     unlink("sjcd.o.gz");
  187.     unlink("sonycd535.o.gz");
  188.     unlink("optcd.o.gz");
  189.     unlink("gscd.o.gz");
  190.     unlink("aic7xxx.o.gz");
  191.     unlink("eata_dma.o.gz");
  192.     unlink("eata_pio.o.gz");
  193.     unlink("pas_16.o.gz");
  194.     unlink("ultrastor.o.gz");
  195.     unlink("u14-34f.o.gz");
  196.  
  197.     chdir("/");
  198.  
  199.     system("gunzip < /tmp/image/pcmcia.cgz | cpio -iumd --quiet");
  200.  
  201.     newtPopWindow();
  202.  
  203.     winStatus(35, 3, "PCMCIA", "Probing PCMCIA controller...");
  204.     rc = runProgramIO(RUN_NOLOG, "/sbin/probe", args, NULL, &probeOutput);
  205.     newtPopWindow();
  206.  
  207.     if (rc || !probeOutput) {
  208.     errorWindow("PCMCIA probe failed");
  209.     return INST_ERROR;
  210.     }
  211.  
  212.     logMessage("pcmcia probe returned: %s", probeOutput);
  213.  
  214.     if (strstr(probeOutput, "TCIC")) {
  215.     strcpy(pcic, "tcic");
  216.     } else
  217.     strcpy(pcic, "i82365");
  218.     logMessage("pcmcia pcic type: %s", pcic);
  219.  
  220.     winStatus(40, 3, "PCMCIA", "Starting PCMCIA services...");
  221.  
  222.     loadModule("pcmcia_core", DRIVER_PCMCIA, DRIVER_MINOR_NONE, &dl);
  223.     loadModule(pcic, DRIVER_PCMCIA, DRIVER_MINOR_NONE, &dl);
  224.     loadModule("ds", DRIVER_PCMCIA, DRIVER_MINOR_NONE, &dl);
  225.  
  226.     *arg = pcic;
  227.  
  228.     if (!fork()) {
  229.     if (!fork()) {
  230.         execl("/sbin/cardmgr", "/sbin/cardmgr", NULL);
  231.         exit(-1);
  232.     }
  233.     exit(-1);
  234.     }
  235.  
  236.     wait(&status);
  237.  
  238.     /* if cardmgr a chance to get going */
  239.     sleep(5);
  240.  
  241.     newtPopWindow();
  242.  
  243.     return 0;
  244. }
  245.  
  246. void doSuspend(void) {
  247.     pid_t pid;
  248.     int status;
  249.  
  250.     newtSuspend();
  251.     if (!(pid = fork())) {
  252.     printf("\n\nType <exit> to return to the install program.\n\n");
  253.     execl("/bin/sh", "-/bin/sh", NULL);
  254.     perror("error execing /bin/sh");
  255.     sleep(5);
  256.     exit(1);
  257.     }
  258.     waitpid(pid, &status, 0);
  259.     newtResume();
  260. }
  261.  
  262. void main(int argc, char ** argv) {
  263.     char ** argptr;
  264.     struct installMethod * method;
  265.     int rc;
  266.     char * pcmciaArg = NULL;
  267.     int isRescue = 0;
  268.     int isSerial;
  269.     int force = 0;
  270.     struct stat sb;
  271.  
  272.     argptr = argv + 1;
  273.     while (*argptr) {
  274.     if (!strcmp(*argptr, "--test")) {
  275.         testing = 1;
  276.     } else if (!strcmp(*argptr, "--force")) {
  277.         force = 1;
  278.     } else if (!strcmp(*argptr, "--rescue")) {
  279.         isRescue = 1;
  280.     }
  281.  
  282.     argptr++;
  283.     }
  284.  
  285.     if (!testing && !force && (getpid() > 50)) {
  286.     fprintf(stderr, "you're running me on a live system! that's ");
  287.     fprintf(stderr, "incredibly stupid.\n");
  288.     exit(1);
  289.     }
  290.  
  291.     openLog();
  292.  
  293.     /* see if we're on a serial console -- if so, don't setup a keymap */
  294.     if (fstat(0, &sb)) {
  295.     logMessage("error stat'ing stdin: %s", strerror(errno));
  296.     return;
  297.     }
  298.  
  299.     if (!S_ISCHR(sb.st_mode)) {
  300.     logMessage("stdin isn't a character device!!! ack!");
  301.     return;
  302.     }
  303.  
  304.     isSerial = (major(sb.st_rdev) == 4 && minor(sb.st_dev) >= 64) ||
  305.                (major(sb.st_rdev) == 5 && minor(sb.st_dev) >= 64);
  306.  
  307.     if (!isRescue) 
  308.     logMessage("welcome to the Red Hat install "
  309.            "(first stage, version " VERSION " built " __DATE__ " "
  310.            __TIME__")");
  311.  
  312.     setenv("NEWT_MONO", "1", 1);
  313.  
  314.     newtInit();
  315.     newtCls();
  316.  
  317.     if (!access("/bin/sh", X_OK)) {
  318.     newtSetSuspendCallback(doSuspend);
  319.     }
  320.  
  321.     newtDrawRootText(0, 0, "Welcome to Red Hat Linux");
  322.  
  323.     setColorState();
  324.  
  325.     newtFinished();
  326.  
  327.     newtInit();
  328.     newtCls();
  329.     newtDrawRootText(0, 0, "Welcome to Red Hat Linux");
  330.  
  331.     if (!isRescue)
  332.     welcome();
  333.  
  334.     newtPushHelpLine(NULL);
  335.  
  336.     #ifndef __sparc__
  337.     if (!isSerial) setupKeyboard();
  338.     #endif
  339.  
  340.     do {
  341.     rc = setupPCMCIA(&pcmciaArg);
  342.     } while (rc);
  343.  
  344.     if (isRescue) {
  345.     do {
  346.         rc = floppyRoot(NULL);
  347.     } while (rc);
  348.     } else {
  349.     chooseInstallMethod(&method);
  350.  
  351. #if 0
  352.     if (!testing) {
  353.         /* set up some symlinks */
  354.         symlink("/tmp/image/RedHat/instimage/lib", "/tmp/lib");
  355.         if (symlink("/tmp/image/RedHat/instimage/usr/bin", "/tmp/bin")) {
  356.         messageWindow("Error", perrorstr("symlink 2"));
  357.         }
  358.     }
  359. #endif
  360.     }
  361.  
  362.     mountHackdisk();
  363.  
  364.     newtFinished();
  365.  
  366.     closeLog();
  367.  
  368.     if (testing) exit(0);
  369.  
  370.     if (isRescue) 
  371.     execl("/usr/bin/runinstall2", "runinstall2", "--rescue", NULL);
  372.     else if (pcmciaArg) 
  373.     execl("/usr/bin/runinstall2", "runinstall2", "--method", 
  374.         method->abbrev, "--pcmcia", pcmciaArg, NULL);
  375.     else
  376.     execl("/usr/bin/runinstall2", "runinstall2", "--method", 
  377.         method->abbrev, NULL);
  378.  
  379.     rc = errno;
  380.     openLog();
  381.     logMessage("error in exec of second stage loader :-(");
  382.     logMessage("\terror:%s", strerror(rc));
  383.  
  384.     while (1) ;
  385.  
  386.     exit(0);
  387. }
  388.