srmbootraw.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * SRMbootraw - SRM boot block composer for raw boot partitions.
  3. * Copyright (C) 1998 Nikita Schmidt <cetus@snowball.ucd.ie>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <fcntl.h>
  19. #include <sys/types.h>
  20. /* We assume little endiannes and possibly other Alpha properties as well. */
  21. #ifndef __alpha__
  22. #error This stuff must be compiled for Alpha.
  23. #endif
  24. #define BUFSIZE 65536
  25. union bootsector {
  26. struct {
  27. unsigned char textlabel[64];
  28. unsigned char disklabel[276];
  29. unsigned char unused[140];
  30. u_int64_t count, start, flags;
  31. u_int64_t checksum;
  32. } srm;
  33. struct {
  34. u_int64_t contents[63];
  35. u_int64_t checksum;
  36. } check;
  37. };
  38. int main (int argc, char *argv[])
  39. {
  40. int device, image;
  41. int i;
  42. union bootsector boot;
  43. char *buf;
  44. unsigned long size;
  45. ssize_t len;
  46. u_int64_t checksum;
  47. if (argc != 3)
  48. return printf ("Usage: srmbootraw <boot device> <boot image>\n"), 1;
  49. if ((device = open (argv[1], O_RDWR)) < 0)
  50. return perror (argv[1]), 2;
  51. if ((image = open (argv[2], O_RDONLY)) < 0)
  52. return perror (argv[2]), 2;
  53. /* Read in the old bootsector */
  54. if (read (device, &boot, sizeof boot) != sizeof boot)
  55. return fprintf (stderr, "Can't read boot sector from %s\n", argv[1]), 2;
  56. if (!(buf = malloc (BUFSIZE)))
  57. return fprintf (stderr, "Can't allocate memory for %s\n", argv[2]), 2;
  58. /* Copy image onto the device */
  59. size = 0;
  60. while ((len = read (image, buf, BUFSIZE)) > 0)
  61. {
  62. if (write (device, buf, len) != len)
  63. return fprintf (stderr, "Can't write to %s\n", argv[1]), 2;
  64. size += len;
  65. }
  66. close (image);
  67. if (len == -1)
  68. return perror (argv[2]), 2;
  69. /* Fill in the bootstrap information */
  70. boot.srm.start = 1;
  71. boot.srm.count = (size + 511) / 512; /* Convert to sectors */
  72. boot.srm.flags = 0;
  73. /* Put the checksum and write the boot sector. */
  74. checksum = 0;
  75. for (i = 0; i < 63; i++)
  76. checksum += boot.check.contents[i];
  77. boot.check.checksum = checksum;
  78. printf ("Writing SRM boot block: starting sector %u, block count %u\n",
  79. (unsigned)boot.srm.start, (unsigned)boot.srm.count);
  80. if (lseek (device, 0, SEEK_SET) == -1
  81. || write (device, &boot, sizeof boot) != sizeof boot)
  82. return perror (argv[2]), 2;
  83. close (device);
  84. return 0;
  85. }