hostdisk.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2004,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB 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 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB 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. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config-util.h>
  19. #include <grub/disk.h>
  20. #include <grub/partition.h>
  21. #include <grub/msdos_partition.h>
  22. #include <grub/types.h>
  23. #include <grub/err.h>
  24. #include <grub/emu/misc.h>
  25. #include <grub/emu/hostdisk.h>
  26. #include <grub/emu/getroot.h>
  27. #include <grub/misc.h>
  28. #include <grub/i18n.h>
  29. #include <grub/list.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <ctype.h>
  34. #include <assert.h>
  35. #include <unistd.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <fcntl.h>
  39. #include <errno.h>
  40. #include <limits.h>
  41. #include <hurd.h>
  42. #include <hurd/lookup.h>
  43. #include <hurd/fs.h>
  44. #include <sys/mman.h>
  45. int
  46. grub_util_hurd_get_disk_info (const char *dev, grub_uint32_t *secsize, grub_disk_addr_t *offset,
  47. grub_disk_addr_t *size, char **parent)
  48. {
  49. file_t file;
  50. mach_port_t *ports;
  51. int *ints;
  52. loff_t *offsets;
  53. char *data;
  54. error_t err;
  55. mach_msg_type_number_t num_ports = 0, num_ints = 0, num_offsets = 0, data_len = 0;
  56. file = file_name_lookup (dev, 0, 0);
  57. if (file == MACH_PORT_NULL)
  58. return 0;
  59. err = file_get_storage_info (file,
  60. &ports, &num_ports,
  61. &ints, &num_ints,
  62. &offsets, &num_offsets,
  63. &data, &data_len);
  64. if (num_ints < 1)
  65. grub_util_error (_("Storage information for `%s' does not include type"), dev);
  66. if (ints[0] != STORAGE_DEVICE)
  67. grub_util_error (_("`%s' is not a local disk"), dev);
  68. if (num_offsets != 2)
  69. grub_util_error (_("Storage information for `%s' indicates neither a plain partition nor a plain disk"), dev);
  70. if (parent)
  71. {
  72. *parent = NULL;
  73. if (num_ints >= 5)
  74. {
  75. size_t len = ints[4];
  76. if (len > data_len)
  77. len = data_len;
  78. *parent = xmalloc (len+1);
  79. memcpy (*parent, data, len);
  80. (*parent)[len] = '\0';
  81. if ((*parent)[0] == '@')
  82. {
  83. /*
  84. * Non-bootstrap disk driver, the /dev/ entry is normally set up with
  85. * the same @.
  86. */
  87. char *next_path = strchr (*parent, ':');
  88. if (next_path)
  89. {
  90. char *n = xstrdup (next_path + 1);
  91. free (*parent);
  92. *parent = n;
  93. }
  94. }
  95. }
  96. }
  97. if (offset)
  98. *offset = offsets[0];
  99. if (size)
  100. *size = offsets[1];
  101. if (secsize)
  102. *secsize = ints[2];
  103. if (ports && num_ports > 0)
  104. {
  105. mach_msg_type_number_t i;
  106. for (i = 0; i < num_ports; i++)
  107. {
  108. mach_port_t port = ports[i];
  109. if (port != MACH_PORT_NULL)
  110. mach_port_deallocate (mach_task_self(), port);
  111. }
  112. munmap ((caddr_t) ports, num_ports * sizeof (*ports));
  113. }
  114. if (ints && num_ints > 0)
  115. munmap ((caddr_t) ints, num_ints * sizeof (*ints));
  116. if (offsets && num_offsets > 0)
  117. munmap ((caddr_t) offsets, num_offsets * sizeof (*offsets));
  118. if (data && data_len > 0)
  119. munmap (data, data_len);
  120. mach_port_deallocate (mach_task_self (), file);
  121. return 1;
  122. }
  123. grub_int64_t
  124. grub_util_get_fd_size_os (grub_util_fd_t fd, const char *name, unsigned *log_secsize)
  125. {
  126. grub_uint32_t sector_size;
  127. grub_disk_addr_t size;
  128. unsigned log_sector_size;
  129. if (!grub_util_hurd_get_disk_info (name, &sector_size, NULL, &size, NULL))
  130. return -1;
  131. if (sector_size & (sector_size - 1) || !sector_size)
  132. return -1;
  133. log_sector_size = grub_log2ull (sector_size);
  134. if (log_secsize)
  135. *log_secsize = log_sector_size;
  136. return size << log_sector_size;
  137. }
  138. void
  139. grub_hostdisk_flush_initial_buffer (const char *os_dev __attribute__ ((unused)))
  140. {
  141. }