home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / sys / hp300 / stand / mkboot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-05  |  6.0 KB  |  250 lines

  1. /*
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  *
  33.  *    @(#)mkboot.c    7.2 (Berkeley) 12/16/90
  34.  */
  35.  
  36. #ifndef lint
  37. char copyright[] =
  38. "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
  39.  All rights reserved.\n";
  40. #endif /* not lint */
  41.  
  42. #ifndef lint
  43. static char sccsid[] = "@(#)mkboot.c    7.2 (Berkeley) 12/16/90";
  44. #endif /* not lint */
  45.  
  46. #include "../include/param.h"
  47. #include "volhdr.h"
  48. #include <sys/exec.h>
  49. #include <sys/file.h>
  50. #include <stdio.h>
  51. #include <ctype.h>
  52.  
  53. int lpflag;
  54. int loadpoint;
  55. struct load ld;
  56. struct lifvol lifv;
  57. struct lifdir lifd[8];
  58. struct exec ex;
  59. char buf[10240];
  60.  
  61. main(argc, argv)
  62.     char **argv;
  63. {
  64.     int ac;
  65.     char **av;
  66.     int from1, from2, to;
  67.     register int n;
  68.     char *n1, *n2, *lifname();
  69.  
  70.     ac = --argc;
  71.     av = ++argv;
  72.     if (ac == 0)
  73.         usage();
  74.     if (!strcmp(av[0], "-l")) {
  75.         av++;
  76.         ac--;
  77.         if (ac == 0)
  78.             usage();
  79.         sscanf(av[0], "0x%x", &loadpoint);
  80.         lpflag++;
  81.         av++;
  82.         ac--;
  83.     }
  84.     if (ac == 0)
  85.         usage();
  86.     from1 = open(av[0], O_RDONLY, 0);
  87.     if (from1 < 0) {
  88.         perror("open");
  89.         exit(1);
  90.     }
  91.     n1 = av[0];
  92.     av++;
  93.     ac--;
  94.     if (ac == 0)
  95.         usage();
  96.     if (ac == 2) {
  97.         from2 = open(av[0], O_RDONLY, 0);
  98.         if (from2 < 0) {
  99.             perror("open");
  100.             exit(1);
  101.         }
  102.         n2 = av[0];
  103.         av++;
  104.         ac--;
  105.     } else
  106.         from2 = -1;
  107.     to = open(av[0], O_WRONLY | O_TRUNC | O_CREAT, 0644);
  108.     if (to < 0) {
  109.         perror("open");
  110.         exit(1);
  111.     }
  112.     /* clear possibly unused directory entries */
  113.     strncpy(lifd[1].dir_name, "          ", 10);
  114.     lifd[1].dir_type = -1;
  115.     lifd[1].dir_addr = 0;
  116.     lifd[1].dir_length = 0;
  117.     lifd[1].dir_flag = 0xFF;
  118.     lifd[1].dir_exec = 0;
  119.     lifd[7] = lifd[6] = lifd[5] = lifd[4] = lifd[3] = lifd[2] = lifd[1];
  120.     /* record volume info */
  121.     lifv.vol_id = VOL_ID;
  122.     strncpy(lifv.vol_label, "BOOT43", 6);
  123.     lifv.vol_addr = 2;
  124.     lifv.vol_oct = VOL_OCT;
  125.     lifv.vol_dirsize = 1;
  126.     lifv.vol_version = 1;
  127.     /* output bootfile one */
  128.     lseek(to, 3 * SECTSIZE, 0);
  129.     putfile(from1, to);
  130.     n = (ld.count + sizeof(ld) + (SECTSIZE - 1)) / SECTSIZE;
  131.     strcpy(lifd[0].dir_name, lifname(n1));
  132.     lifd[0].dir_type = DIR_TYPE;
  133.     lifd[0].dir_addr = 3;
  134.     lifd[0].dir_length = n;
  135.     lifd[0].dir_flag = DIR_FLAG;
  136.     lifd[0].dir_exec = lpflag? loadpoint + ex.a_entry : ex.a_entry;
  137.     lifv.vol_length = lifd[0].dir_addr + lifd[0].dir_length;
  138.     /* if there is an optional second boot program, output it */
  139.     if (from2 >= 0) {
  140.         lseek(to, (3 + n) * SECTSIZE, 0);
  141.         putfile(from2, to);
  142.         n = (ld.count + sizeof(ld) + (SECTSIZE - 1)) / SECTSIZE;
  143.         strcpy(lifd[1].dir_name, lifname(n2));
  144.         lifd[1].dir_type = DIR_TYPE;
  145.         lifd[1].dir_addr = 3 + lifd[0].dir_length;
  146.         lifd[1].dir_length = n;
  147.         lifd[1].dir_flag = DIR_FLAG;
  148.         lifd[1].dir_exec = lpflag? loadpoint + ex.a_entry : ex.a_entry;
  149.         lifv.vol_length = lifd[1].dir_addr + lifd[1].dir_length;
  150.     }
  151.     /* output volume/directory header info */
  152.     lseek(to, 0 * SECTSIZE, 0);
  153.     write(to, &lifv, sizeof(lifv));
  154.     lseek(to, 2 * SECTSIZE, 0);
  155.     write(to, lifd, sizeof(lifd));
  156.     exit(0);
  157. }
  158.  
  159. putfile(from, to)
  160. {
  161.     register int n, tcnt, dcnt;
  162.  
  163.     n = read(from, &ex, sizeof(ex));
  164.     if (n != sizeof(ex)) {
  165.         fprintf(stderr, "error reading file header\n");
  166.         exit(1);
  167.     }
  168.     if (ex.a_magic == OMAGIC) {
  169.         tcnt = ex.a_text;
  170.         dcnt = ex.a_data;
  171.     }
  172.     else if (ex.a_magic == NMAGIC) {
  173.         tcnt = (ex.a_text + PGOFSET) & ~PGOFSET;
  174.         dcnt = ex.a_data;
  175.     }
  176.     else {
  177.         fprintf(stderr, "bad magic number\n");
  178.         exit(1);
  179.     }
  180.     ld.address = lpflag ? loadpoint : ex.a_entry;
  181.     ld.count = tcnt + dcnt;
  182.     write(to, &ld, sizeof(ld));
  183.     while (tcnt) {
  184.         n = sizeof(buf);
  185.         if (n > tcnt)
  186.             n = tcnt;
  187.         n = read(from, buf, n);
  188.         if (n < 0) {
  189.             perror("read");
  190.             exit(1);
  191.         }
  192.         if (n == 0) {
  193.             fprintf(stderr, "short read\n");
  194.             exit(1);
  195.         }
  196.         if (write(to, buf, n) < 0) {
  197.             perror("write");
  198.             exit(1);
  199.         }
  200.         tcnt -= n;
  201.     }
  202.     while (dcnt) {
  203.         n = sizeof(buf);
  204.         if (n > dcnt)
  205.             n = dcnt;
  206.         n = read(from, buf, n);
  207.         if (n < 0) {
  208.             perror("read");
  209.             exit(1);
  210.         }
  211.         if (n == 0) {
  212.             fprintf(stderr, "short read\n");
  213.             exit(1);
  214.         }
  215.         if (write(to, buf, n) < 0) {
  216.             perror("write");
  217.             exit(1);
  218.         }
  219.         dcnt -= n;
  220.     }
  221. }
  222.  
  223. usage()
  224. {
  225.     fprintf(stderr,
  226.         "usage:  mkboot [-l loadpoint] prog1 [ prog2 ] outfile\n");
  227.     exit(1);
  228. }
  229.  
  230. char *
  231. lifname(str)
  232.  char *str;
  233. {
  234.     static char lname[10] = "SYS_XXXXX";
  235.     register int i;
  236.  
  237.     for (i = 4; i < 9; i++) {
  238.         if (islower(*str))
  239.             lname[i] = toupper(*str);
  240.         else if (isalnum(*str) || *str == '_')
  241.             lname[i] = *str;
  242.         else
  243.             break;
  244.         str++;
  245.     }
  246.     for ( ; i < 10; i++)
  247.         lname[i] = '\0';
  248.     return(lname);
  249. }
  250.