0205-normal-main-Discover-the-device-to-read-the-config-f.patch 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Javier Martinez Canillas <javierm@redhat.com>
  3. Date: Mon, 30 Aug 2021 12:31:18 +0200
  4. Subject: [PATCH] normal/main: Discover the device to read the config from as a
  5. fallback
  6. The GRUB core.img is generated locally, when this is done the grub2-probe
  7. tool figures out the device and partition that needs to be read to parse
  8. the GRUB configuration file.
  9. But in some cases the core.img can't be generated on the host and instead
  10. has to be done at package build time. For example, if needs to get signed
  11. with a key that's only available on the package building infrastructure.
  12. If that's the case, the prefix variable won't have a device and partition
  13. but only a directory path. So there's no way for GRUB to know from which
  14. device has to read the configuration file.
  15. To allow GRUB to continue working on that scenario, fallback to iterating
  16. over all the available devices, if reading the config failed when using
  17. the prefix and fw_path variables.
  18. Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
  19. ---
  20. grub-core/normal/main.c | 58 +++++++++++++++++++++++++++++++++++++++++++------
  21. 1 file changed, 51 insertions(+), 7 deletions(-)
  22. diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c
  23. index 4ebdbd228d4..b72fe3d653c 100644
  24. --- a/grub-core/normal/main.c
  25. +++ b/grub-core/normal/main.c
  26. @@ -337,18 +337,13 @@ grub_enter_normal_mode (const char *config)
  27. }
  28. static grub_err_t
  29. -grub_try_normal (const char *variable)
  30. +grub_try_normal_prefix (const char *prefix)
  31. {
  32. char *config;
  33. - const char *prefix;
  34. grub_err_t err = GRUB_ERR_FILE_NOT_FOUND;
  35. const char *net_search_cfg;
  36. int disable_net_search = 0;
  37. - prefix = grub_env_get (variable);
  38. - if (!prefix)
  39. - return GRUB_ERR_FILE_NOT_FOUND;
  40. -
  41. net_search_cfg = grub_env_get ("feature_net_search_cfg");
  42. if (net_search_cfg && net_search_cfg[0] == 'n')
  43. disable_net_search = 1;
  44. @@ -362,7 +357,7 @@ grub_try_normal (const char *variable)
  45. config = grub_malloc (config_len);
  46. if (! config)
  47. - return GRUB_ERR_FILE_NOT_FOUND;
  48. + return err;
  49. grub_snprintf (config, config_len, "%s/grub.cfg", prefix);
  50. err = grub_net_search_config_file (config);
  51. @@ -391,6 +386,53 @@ grub_try_normal (const char *variable)
  52. return err;
  53. }
  54. +static int
  55. +grub_try_normal_dev (const char *name, void *data)
  56. +{
  57. + grub_err_t err;
  58. + const char *prefix = grub_xasprintf ("(%s)%s", name, (char *)data);
  59. +
  60. + if (!prefix)
  61. + return 0;
  62. +
  63. + err = grub_try_normal_prefix (prefix);
  64. + if (err == GRUB_ERR_NONE)
  65. + return 1;
  66. +
  67. + return 0;
  68. +}
  69. +
  70. +static grub_err_t
  71. +grub_try_normal_discover (void)
  72. +{
  73. + char *prefix = grub_env_get ("prefix");
  74. + grub_err_t err = GRUB_ERR_FILE_NOT_FOUND;
  75. +
  76. + if (!prefix)
  77. + return err;
  78. +
  79. + if (grub_device_iterate (grub_try_normal_dev, (void *)prefix))
  80. + return GRUB_ERR_NONE;
  81. +
  82. + return err;
  83. +}
  84. +
  85. +static grub_err_t
  86. +grub_try_normal (const char *variable)
  87. +{
  88. + grub_err_t err = GRUB_ERR_FILE_NOT_FOUND;
  89. + const char *prefix;
  90. +
  91. + if (!variable)
  92. + return err;
  93. +
  94. + prefix = grub_env_get (variable);
  95. + if (!prefix)
  96. + return err;
  97. +
  98. + return grub_try_normal_prefix (prefix);
  99. +}
  100. +
  101. /* Enter normal mode from rescue mode. */
  102. static grub_err_t
  103. grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
  104. @@ -405,6 +447,8 @@ grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
  105. err = grub_try_normal ("fw_path");
  106. if (err == GRUB_ERR_FILE_NOT_FOUND)
  107. err = grub_try_normal ("prefix");
  108. + if (err == GRUB_ERR_FILE_NOT_FOUND)
  109. + err = grub_try_normal_discover ();
  110. if (err == GRUB_ERR_FILE_NOT_FOUND)
  111. grub_enter_normal_mode (0);
  112. }