Skip to content

Commit

Permalink
efi: new 'connectefi' command
Browse files Browse the repository at this point in the history
Integrated Glen's comments (except 'connectefi'):
- use grub_list instead of own list
- added 'all' option

Signed-off-by: Renaud Métrich <[email protected]>
  • Loading branch information
rmetrich committed Aug 31, 2022
1 parent f08660d commit d54fc59
Showing 1 changed file with 41 additions and 35 deletions.
76 changes: 41 additions & 35 deletions grub-core/commands/efi/connectefi.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,31 @@

GRUB_MOD_LICENSE ("GPLv3+");

typedef struct handle_list
struct grub_efi_already_handled
{
struct grub_efi_already_handled *next;
struct grub_efi_already_handled **prev;
grub_efi_handle_t handle;
struct handle_list *next;
} handle_list_t;
};

static handle_list_t *already_handled = NULL;
static struct grub_efi_already_handled *already_handled;

static grub_err_t
add_handle (grub_efi_handle_t handle)
{
handle_list_t *e;
e = grub_malloc (sizeof (*e));
if (! e)
return grub_errno;
e->handle = handle;
e->next = already_handled;
already_handled = e;
return GRUB_ERR_NONE;
}

static int
static struct grub_efi_already_handled *
is_in_list (grub_efi_handle_t handle)
{
handle_list_t *e;
for (e = already_handled; e != NULL; e = e->next)
struct grub_efi_already_handled *e;

FOR_LIST_ELEMENTS (e, already_handled)
if (e->handle == handle)
return 1;
return 0;
return e;

return NULL;
}

static void
free_handle_list (void)
{
handle_list_t *e;
struct grub_efi_already_handled *e;
while ((e = already_handled) != NULL)
{
already_handled = already_handled->next;
Expand Down Expand Up @@ -105,16 +95,21 @@ grub_cmd_connectefi (grub_command_t cmd __attribute__ ((unused)),
if (argc != 1)
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));

if (grub_strcmp(args[0], N_("pciroot")) == 0)
if (grub_strcmp(args[0], "pciroot") == 0)
{
items = pciroot_items;
nitems = ARRAY_SIZE (pciroot_items);
}
else if (grub_strcmp(args[0], N_("scsi")) == 0)
else if (grub_strcmp(args[0], "scsi") == 0)
{
items = scsi_items;
nitems = ARRAY_SIZE (scsi_items);
}
else if (grub_strcmp(args[0], N_("all")) == 0)
{
items = NULL;
nitems = 1;
}
else
return grub_error (GRUB_ERR_BAD_ARGUMENT,
N_("unexpected argument `%s'"), args[0]);
Expand All @@ -127,10 +122,15 @@ grub_cmd_connectefi (grub_command_t cmd __attribute__ ((unused)),

loop:
loop++;
grub_dprintf ("efi", "step '%s' loop %d:\n", items[s].name, loop);

handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL,
&items[s].guid, 0, &num_handles);
if (items != NULL)
{
grub_dprintf ("efi", "step '%s' loop %d:\n", items[s].name, loop);
handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL,
&items[s].guid, 0, &num_handles);
}
else
handles = grub_efi_locate_handle (GRUB_EFI_ALL_HANDLES,
NULL, NULL, &num_handles);

if (!handles)
continue;
Expand All @@ -142,15 +142,15 @@ grub_cmd_connectefi (grub_command_t cmd __attribute__ ((unused)),
unsigned j;

/* Skip already handled handles */
if (is_in_list (handle))
if (is_in_list (handle) != NULL)
{
grub_dprintf ("efi", " handle %p: already processed\n",
handle);
continue;
}

status = grub_efi_connect_controller(handle, NULL, NULL,
items[s].flags & SEARCHED_ITEM_FLAG_RECURSIVE ? 1 : 0);
!items || items[s].flags & SEARCHED_ITEM_FLAG_RECURSIVE ? 1 : 0);
if (status == GRUB_EFI_SUCCESS)
{
connected++;
Expand All @@ -161,15 +161,18 @@ grub_cmd_connectefi (grub_command_t cmd __attribute__ ((unused)),
grub_dprintf ("efi", " handle %p: failed to connect (%d)\n",
handle, (grub_efi_int8_t) status);

if ((grub_err = add_handle (handle)) != GRUB_ERR_NONE)
struct grub_efi_already_handled *item = grub_malloc(sizeof (*item));
if (!item)
break; /* fatal */
grub_list_push (GRUB_AS_LIST_P (&already_handled), GRUB_AS_LIST (item));
item->handle = handle;
}

grub_free (handles);
if (grub_err != GRUB_ERR_NONE)
break; /* fatal */

if (items[s].flags & SEARCHED_ITEM_FLAG_LOOP && connected)
if (items && items[s].flags & SEARCHED_ITEM_FLAG_LOOP && connected)
{
connected = 0;
goto loop;
Expand All @@ -191,12 +194,15 @@ static grub_command_t cmd;
GRUB_MOD_INIT(connectefi)
{
cmd = grub_register_command ("connectefi", grub_cmd_connectefi,
N_("pciroot|scsi"),
/* TRANSLATORS: only translate 'all' keyword */
N_("pciroot|scsi|all"),
N_("Connect EFI handles."
" If 'pciroot' is specified, connect PCI"
" root EFI handles recursively."
" If 'scsi' is specified, connect SCSI"
" I/O EFI handles recursively."));
" I/O EFI handles recursively."
" If 'all' is specified, connect all"
" EFI handles recursively (use with care)."));
}

GRUB_MOD_FINI(connectefi)
Expand Down

0 comments on commit d54fc59

Please sign in to comment.