home *** CD-ROM | disk | FTP | other *** search
/ Caldera Network Desktop 1.0 / caldera-network-desktop-1.0.bin / images / ramdisk2-beta.img / usr / lib / perl / floppy < prev    next >
Text File  |  1995-12-04  |  8KB  |  331 lines

  1. $current_floppy = "";
  2.  
  3. open(LOG, ">/dev/null");
  4.  
  5. $floppy_header_length = 4 * 4;
  6. $floppy_size = (1440*1024);
  7. $floppy_amount = $floppy_size - $floppy_header_length;
  8.  
  9. sub floppy_disk_prompt {
  10.     local ($filevar, $diskname, $realmagic, $realsig) = @_;
  11.     local ($gotit, $diskmagic, $disksig, $allmagic, $seriesnum, $disknum);
  12.     local ($version, $timestamp);
  13.  
  14.     if ($current_floppy eq $diskname) { 
  15.     open($filevar, "</dev/fd0") && do {
  16.         return 0;
  17.     }
  18.     }
  19.  
  20.     &rhs_msgbox ( "Insert Floppy",
  21. <<EOM
  22. >
  23. Now please remove the floppy from the first drive and insert 
  24. $diskname into /dev/fd0 (DOS drive A:). 
  25. >
  26. Press <Enter> when ready.
  27. >
  28. EOM
  29.          , 70);
  30.  
  31.     $gotit = 0;
  32.     while (! $gotit) {
  33.     open($filevar, "</dev/fd0") || do {
  34.         &rhs_msgbox ( "No Floppy",
  35. <<EOM
  36. >
  37. The isn't any disk in /dev/fd0 (DOS drive A:), or the
  38. disk you're using is bad.
  39. >
  40. Press <Enter> to try again.
  41. >
  42. EOM
  43.          , 70);
  44.         next;
  45.     } ;
  46.  
  47.     (8 == read($filevar, $allmagic, 8)) || do {
  48.         &rhs_msgbox ( "Bad Floppy",
  49. <<EOM
  50. >
  51. I can't read anything from that disk!
  52. >
  53. Press <Enter> to try again.
  54. >
  55. EOM
  56.          , 70); 
  57.         next;
  58.     } ;
  59.  
  60.     ($diskmagic, $disksig) = unpack("NN", $allmagic);
  61.  
  62.     if ($diskmagic != $realmagic) {
  63.         &rhs_msgbox ( "Bad Floppy",
  64. <<EOM
  65. >
  66. That's not a Red Hat floppy!
  67. >
  68. Press <Enter> to try again.
  69. >
  70. EOM
  71.          , 70);
  72.         next;
  73.     } ;
  74.  
  75.     if ($disksig != $realsig) {
  76.         if ($disksig == 0xFFFFFFFF) {
  77.         &rhs_msgbox ( "Wrong Floppy",
  78. <<EOM
  79. >
  80. You inserted the main Red Hat disk. Please 
  81. insert $diskname.
  82. >
  83. Press <Enter> to try again.
  84. >
  85. EOM
  86.          , 70);
  87.         next;
  88.         }
  89.  
  90.         $seriesnum = $disksig >> 16;
  91.         $disknum = $disksig & 0xFFFF;
  92.         
  93.         if ($floppy_series_names[$seriesnum]) {
  94.         &rhs_msgbox ( "Wrong Floppy",
  95. <<EOM
  96. >
  97. You inserted the wrong Red Hat floppy. You inserted disk 
  98. $disknum from series $floppy_series_names[$seriesnum]. Insert
  99. $diskname instead.
  100. >
  101. Press <Enter> to try again.
  102. >
  103. EOM
  104.          , 70);
  105.         next;
  106.         } else {
  107.         &rhs_msgbox ( "Wrong Floppy",
  108. <<EOM
  109. >
  110. You inserted the wrong Red Hat floppy. Insert
  111. $diskname instead.
  112. >
  113. Press <Enter> to try again.
  114. >
  115. EOM
  116.          , 70);
  117.         next;
  118.         }
  119.     }
  120.  
  121.     if ($realsig != 0xFFFFFFFF) {
  122.         (8 == read($filevar, $allmagic, 8)) || do {
  123.         &rhs_msgbox ( "Bad Floppy",
  124. <<EOM
  125. >
  126. I can't read from that disk!
  127. >
  128. Press <Enter> to try again.
  129. >
  130. EOM
  131.          , 70); 
  132.         next;
  133.         } ;
  134.  
  135.         ($version, $timestamp) = unpack("NN", $allmagic);
  136.         if ($timestamp != $floppy_timestamp) {
  137.         &rhs_msgbox ( "Wrong Floppy",
  138. <<EOM
  139. >
  140. That floppy is from a different Red Hat
  141. release then the first floppy.
  142. >
  143. Press <Enter> to try again.
  144. >
  145. EOM
  146.          , 70); 
  147.         next;
  148.         }
  149.     }
  150.  
  151.     $gotit = 1;
  152.     }
  153.  
  154.     seek(FLDEVICE, 0, 0);  # just like we never touched it
  155.  
  156.     $current_floppy = $diskname;
  157.  
  158.     return 0;
  159. }
  160.  
  161. sub floppy_read_header {
  162.     local ($series_file_name) = @_;
  163.     local ($header, $magic, $version, $series_table_length, $series_table_off);
  164.     local ($file_table, $series_table, $file_table_offset, $file_table_length);
  165.     local ($series_file, $series_file_length, $series_file_offset);
  166.     local ($hlen, $strlen, $moremagic);
  167.  
  168.     $hlen = 10 * 4;
  169.  
  170.     &floppy_disk_prompt(FLDEVICE, "the main floppy", 0x3245eadc, 0xFFFFFFFF);
  171.  
  172.     ($hlen == read(FLDEVICE, $header, $hlen)) || 
  173.     die "cannot read header from /dev/fd0";
  174.  
  175.     ($magic, $moremagic, $version, $floppy_timestamp, $series_table_length, 
  176.         $series_table_offset, $file_table_length, $file_table_offset, 
  177.     $series_file_length, $series_file_offset) = 
  178.         unpack("NNNNNNNNNN", $header);
  179.  
  180.     ($magic == 0x3245eadc) || die "bad header magic on /dev/fd0";
  181.     ($moremagic == 0xFFFFFFFF) || die "bad extra magic on /dev/fd0";
  182.     ($version == 1) || die "I can only read version 1 floppies"; 
  183.  
  184.     ($series_table_length == read(FLDEVICE, $series_table, $series_table_length))
  185.     || die "cannot read series table from /dev/fd0";
  186.  
  187.     ($file_table_length == read(FLDEVICE, $file_table, $file_table_length)) || 
  188.     die "cannot read file table from /dev/fd0";
  189.  
  190.     ($series_file_length == read(FLDEVICE, $series_file, $series_file_length)) ||
  191.     die "cannot read series file from /dev/fd0";
  192.  
  193.     close(FLDEVICE);
  194.  
  195.     open(F, ">$series_file_name") || die "cannot create $series_file_name";
  196.     print F $series_file;
  197.     close(F);
  198.  
  199.     # put together the series array
  200.  
  201.     undef(@floppy_series_names);
  202.     while ($series_table) {
  203.     $strlen = unpack("N", $series_table);
  204.     push(@floppy_series_names, substr($series_table, 4, $strlen));
  205.     $series_table = substr($series_table, 4 + $strlen);
  206.     }
  207.  
  208.     # a filename has to be enough to give us the series and offset the file
  209.     # is located at
  210.  
  211.     undef(@floppy_file_list);
  212.  
  213.     while ($file_table) {
  214.     $strlen = unpack("N", $file_table);
  215.     $filename = substr($file_table, 4, $strlen);
  216.  
  217.     $file_table = substr($file_table, 4 + $strlen);
  218.     ($file_size, $file_series_num, $file_offset) = unpack("NNN",
  219.             $file_table);
  220.     $file_table = substr($file_table, 12);
  221.  
  222.     $floppy_file_info{"${filename}:size"} = $file_size;
  223.     $floppy_file_info{"${filename}:series_num"} = $file_series_num;
  224.     $floppy_file_info{"${filename}:offset"} = $file_offset;
  225.  
  226.     push(@floppy_file_list, $filename);
  227.     }
  228.  
  229.     return 1;
  230. }
  231.  
  232. sub floppy_copy_file {
  233.     local ($filename, $dir, *newfloppy) = @_;
  234.     local ($disknum, $size);
  235.  
  236.     $floppy_file_info{"${filename}:size"} || do {
  237.     print STDERR "I can't find file $filename!\n\n";
  238.     return 0;
  239.     } ;
  240.  
  241.     $seriesnum = $floppy_file_info{"${filename}:series_num"};
  242.     $disknum = int(($floppy_file_info{"${filename}:offset"} / $floppy_amount ));
  243.     $size = $floppy_file_info{"${filename}:size"};
  244.     $offset = $floppy_file_info{"${filename}:offset"} - 
  245.            ($disknum * $floppy_amount) + $floppy_header_length;
  246.     $disknum++;
  247.  
  248.     print LOG "file is $filename\n";
  249.     print LOG "series num is $seriesnum\n";
  250.     print LOG "disk num is $disknum\n";
  251.  
  252.     print LOG "absolute offset: ", $floppy_file_info{"${filename}:offset"}, "\n";
  253.     print LOG "offset: ", $offset, "\n";
  254.  
  255.     open(OUTFILE, ">$dir/$filename") || do {
  256.     close(FLDEVICE);
  257.     print STDERR "Error creating $dir/$filename.\n";
  258.     <STDIN>;
  259.     return 0;
  260.     } ;
  261.  
  262.     while ($size) {
  263.     &floppy_disk_prompt(FLDEVICE, "$floppy_series_names[$seriesnum] disk $disknum",
  264.                 0x3245eadc, $seriesnum << 16 | $disknum);
  265.  
  266.     print LOG "reading starts at offset $offset in disk $disknum\n";
  267.     seek(FLDEVICE, $offset, 0);
  268.     if ($size <= ($floppy_size - $offset)) {
  269.         &floppy_copychunk(FLDEVICE, OUTFILE, $size);
  270.         $size = 0;
  271.     } else {
  272.         print LOG "reading $floppy_size - $offset (", $floppy_size - $offset, ") bytes\n";
  273.         &floppy_copychunk(FLDEVICE, OUTFILE, $floppy_size - $offset);
  274.         $size -= $floppy_size - $offset;
  275.         close(FLDEVICE);
  276.         $disknum++;
  277.         $offset = $floppy_header_length;
  278.     }
  279.     }
  280.  
  281.     close(FLDEVICE);
  282.     close(OUTFILE);
  283. }
  284.  
  285. sub floppy_get_rpm_list {
  286.     local (@l);
  287.  
  288.     for $n (@floppy_file_list) {
  289.         if ($n =~ /\.rpm$/) { push(@l, $n); }
  290.     }
  291.  
  292.     return @l;
  293. }
  294.  
  295. sub floppy_rpm_compare {
  296.     if ($floppy_file_info{"${a}:series_num"} < 
  297.         $floppy_file_info{"${b}:series_num"}) { return -1 }
  298.     if ($floppy_file_info{"${a}:series_num"} > 
  299.         $floppy_file_info{"${b}:series_num"}) { return 1 }
  300.     if ($floppy_file_info{"${a}:offset"} < 
  301.         $floppy_file_info{"${b}:offset"}) { return -1 }
  302.  
  303.     return 1;
  304. }
  305.  
  306. sub floppy_sort_rpms {
  307.     return (sort floppy_rpm_compare @_);
  308. }
  309.  
  310. sub floppy_init {
  311.     &umount_bootdisk();
  312.  
  313.     &floppy_read_header("/tmp/series") || return 0;
  314.  
  315.     return 1;
  316. }
  317.  
  318. sub floppy_copychunk {
  319.     local ($from, $to, $size) = @_;
  320.     local ($buffer);
  321.  
  322.     if ($size != read($from, $buffer, $size)) {
  323.     print STDERR "read failed :-(\n";
  324.     <STDIN>;
  325.     }
  326.  
  327.     print $to $buffer;
  328. }
  329.  
  330. 1;
  331.