abootconf.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * abootconf.c
  3. *
  4. * This file is part of aboot, the SRM bootloader for Linux/Alpha
  5. * Copyright (C) 1996 Linus Torvalds, David Mosberger, and Michael Schwingen.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <errno.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <sys/fcntl.h>
  26. #include <config.h>
  27. const char * prog_name;
  28. int
  29. main (int argc, char ** argv)
  30. {
  31. unsigned long sector[512 / sizeof(unsigned long)];
  32. off_t aboot_pos;
  33. size_t nbytes;
  34. long part = -1;
  35. int disk, i;
  36. prog_name = argv[0];
  37. if (argc != 2 && argc != 3) {
  38. fprintf(stderr, "usage: %s device [partition]\n", prog_name);
  39. exit(1);
  40. }
  41. if (argc > 2) {
  42. errno = 0;
  43. part = strtol(argv[2], 0, 10);
  44. if (errno == ERANGE) {
  45. fprintf(stderr, "%s: bad partition number %s\n",
  46. prog_name, argv[2]);
  47. exit(1);
  48. }
  49. }
  50. disk = open(argv[1], part < 0 ? O_RDONLY : O_RDWR);
  51. if (disk < 0) {
  52. perror(argv[1]);
  53. exit(1);
  54. }
  55. nbytes = read(disk, sector, sizeof(sector));
  56. if (nbytes != sizeof(sector)) {
  57. if ((long) nbytes < 0) {
  58. perror("read");
  59. } else {
  60. fprintf(stderr, "%s: short read\n", prog_name);
  61. }
  62. exit(1);
  63. }
  64. aboot_pos = sector[61] * 512;
  65. if (lseek(disk, aboot_pos, SEEK_SET) != aboot_pos) {
  66. perror("lseek");
  67. exit(1);
  68. }
  69. nbytes = read(disk, sector, sizeof(sector));
  70. if (nbytes != sizeof(sector)) {
  71. if ((long) nbytes < 0) {
  72. perror("read");
  73. } else {
  74. fprintf(stderr, "%s: short read\n", prog_name);
  75. }
  76. exit(1);
  77. }
  78. for (i = 0; i < (int) (sizeof(sector)/sizeof(sector[0])); ++i) {
  79. if (sector[i] == ABOOT_MAGIC)
  80. break;
  81. }
  82. if (i >= (int) (sizeof(sector)/sizeof(sector[0]))) {
  83. fprintf(stderr, "%s: could not find aboot on disk %s\n",
  84. prog_name, argv[1]);
  85. exit(1);
  86. }
  87. if (part < 0) {
  88. printf("aboot.conf partition currently set to %ld\n",
  89. sector[i + 1]);
  90. exit(0);
  91. }
  92. if (lseek(disk, aboot_pos, SEEK_SET) != aboot_pos) {
  93. perror("lseek");
  94. exit(1);
  95. }
  96. sector[i + 1] = atoi(argv[2]);
  97. nbytes = write(disk, sector, sizeof(sector));
  98. if (nbytes != sizeof(sector)) {
  99. if ((long) nbytes < 0) {
  100. perror("write");
  101. } else {
  102. fprintf(stderr, "%s: short write\n", prog_name);
  103. }
  104. exit(1);
  105. }
  106. return 0;
  107. }