hostdisk.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. }
  82. }
  83. if (offset)
  84. *offset = offsets[0];
  85. if (size)
  86. *size = offsets[1];
  87. if (secsize)
  88. *secsize = ints[2];
  89. if (ports && num_ports > 0)
  90. {
  91. mach_msg_type_number_t i;
  92. for (i = 0; i < num_ports; i++)
  93. {
  94. mach_port_t port = ports[i];
  95. if (port != MACH_PORT_NULL)
  96. mach_port_deallocate (mach_task_self(), port);
  97. }
  98. munmap ((caddr_t) ports, num_ports * sizeof (*ports));
  99. }
  100. if (ints && num_ints > 0)
  101. munmap ((caddr_t) ints, num_ints * sizeof (*ints));
  102. if (offsets && num_offsets > 0)
  103. munmap ((caddr_t) offsets, num_offsets * sizeof (*offsets));
  104. if (data && data_len > 0)
  105. munmap (data, data_len);
  106. mach_port_deallocate (mach_task_self (), file);
  107. return 1;
  108. }
  109. grub_int64_t
  110. grub_util_get_fd_size_os (grub_util_fd_t fd, const char *name, unsigned *log_secsize)
  111. {
  112. grub_uint32_t sector_size;
  113. grub_disk_addr_t size;
  114. unsigned log_sector_size;
  115. if (!grub_util_hurd_get_disk_info (name, &sector_size, NULL, &size, NULL))
  116. return -1;
  117. if (sector_size & (sector_size - 1) || !sector_size)
  118. return -1;
  119. for (log_sector_size = 0;
  120. (1 << log_sector_size) < sector_size;
  121. log_sector_size++);
  122. if (log_secsize)
  123. *log_secsize = log_sector_size;
  124. return size << log_sector_size;
  125. }
  126. void
  127. grub_hostdisk_flush_initial_buffer (const char *os_dev __attribute__ ((unused)))
  128. {
  129. }