getroot.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <config.h>
  2. #include <config-util.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <stdint.h>
  8. #include <grub/util/misc.h>
  9. #include <grub/emu/hostdisk.h>
  10. #include <grub/emu/getroot.h>
  11. #include <fs_info.h>
  12. #include <Drivers.h>
  13. #include <StorageDefs.h>
  14. enum grub_dev_abstraction_types
  15. grub_util_get_dev_abstraction_os (const char *os_dev __attribute__((unused)))
  16. {
  17. return GRUB_DEV_ABSTRACTION_NONE;
  18. }
  19. int
  20. grub_util_pull_device_os (const char *os_dev __attribute__ ((unused)),
  21. enum grub_dev_abstraction_types ab __attribute__ ((unused)))
  22. {
  23. return 0;
  24. }
  25. char *
  26. grub_util_get_grub_dev_os (const char *os_dev __attribute__ ((unused)))
  27. {
  28. return NULL;
  29. }
  30. char **
  31. grub_guess_root_devices (const char *dir_in)
  32. {
  33. dev_t dv = dev_for_path (dir_in);
  34. fs_info inf;
  35. char **ret;
  36. if (fs_stat_dev (dv, &inf) != B_OK)
  37. return NULL;
  38. ret = xmalloc (2 * sizeof (ret[0]));
  39. ret[0] = xstrdup (inf.device_name);
  40. ret[1] = NULL;
  41. return ret;
  42. }
  43. grub_disk_addr_t
  44. grub_util_find_partition_start_os (const char *dev)
  45. {
  46. partition_info part;
  47. grub_disk_addr_t ret;
  48. int fd = open (dev, O_RDONLY);
  49. if (fd < 0)
  50. return 0;
  51. if (ioctl (fd, B_GET_PARTITION_INFO, &part, sizeof (part)) < 0)
  52. {
  53. close (fd);
  54. return 0;
  55. }
  56. ret = part.offset;
  57. close (fd);
  58. fd = open (part.device, O_RDONLY);
  59. device_geometry geo;
  60. if (ioctl (fd, B_GET_GEOMETRY, &geo, sizeof (geo)) < 0)
  61. return 0;
  62. ret /= geo.bytes_per_sector ? : 512;
  63. close (fd);
  64. return ret;
  65. }
  66. char *
  67. grub_util_part_to_disk (const char *os_dev,
  68. struct stat *st __attribute__ ((unused)),
  69. int *is_part)
  70. {
  71. char *ret;
  72. partition_info part;
  73. int fd = open (os_dev, O_RDONLY);
  74. *is_part = 0;
  75. if (fd < 0)
  76. return xstrdup (os_dev);
  77. if (ioctl (fd, B_GET_PARTITION_INFO, &part, sizeof (part)) < 0)
  78. {
  79. close (fd);
  80. return xstrdup (os_dev);
  81. }
  82. ret = xstrdup (part.device);
  83. close (fd);
  84. *is_part=1;
  85. return ret;
  86. }
  87. int
  88. grub_util_biosdisk_is_floppy (grub_disk_t disk)
  89. {
  90. const char *dname;
  91. dname = grub_util_biosdisk_get_osdev (disk);
  92. return (strncmp (dname, "/dev/disk/floppy/", sizeof ("/dev/disk/floppy/") - 1)
  93. == 0);
  94. }