iPXE
shim_cmd.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <getopt.h>
27 #include <ipxe/command.h>
28 #include <ipxe/parseopt.h>
29 #include <ipxe/efi/efi_image.h>
30 #include <usr/imgmgmt.h>
31 #include <usr/shimmgmt.h>
32 
33 /** @file
34  *
35  * EFI shim command
36  *
37  */
38 
39 /* Exist as a dummy command on non-EFI platforms */
40 #ifdef PLATFORM_efi
41 #define shim_dummy 0
42 #else
43 #define shim_dummy 1
44 #endif
45 
46 /** "shim" options */
47 struct shim_options {
48  /** Download timeout */
49  unsigned long timeout;
50  /** Require third party loader */
52  /** Allow PXE base code protocol */
53  int allow_pxe;
54  /** Allow SBAT variable access */
56 };
57 
58 /** "shim" option list */
59 static struct option_descriptor shim_opts[] = {
60  OPTION_DESC ( "timeout", 't', required_argument,
62  OPTION_DESC ( "require-loader", 'l', no_argument,
63  struct shim_options, require_loader, parse_flag ),
64  OPTION_DESC ( "allow-pxe", 'p', no_argument,
65  struct shim_options, allow_pxe, parse_flag ),
66  OPTION_DESC ( "allow-sbat", 's', no_argument,
67  struct shim_options, allow_sbat, parse_flag ),
68 };
69 
70 /** "shim" command descriptor */
72  COMMAND_DESC ( struct shim_options, shim_opts, 0, 1, NULL );
73 
74 /**
75  * The "shim" command
76  *
77  * @v argc Argument count
78  * @v argv Argument list
79  * @ret rc Return status code
80  */
81 static int shim_exec ( int argc, char **argv ) {
82  struct shim_options opts;
83  struct image *image = NULL;
84  struct image *kernel;
85  char *name_uri;
86  int download;
87  int rc;
88 
89  /* Do absolutely nothing if this is a non-EFI platform */
90  if ( shim_dummy ) {
91  rc = 0;
92  goto err_dummy;
93  }
94 
95  /* Parse options */
96  if ( ( rc = parse_options ( argc, argv, &shim_cmd, &opts ) ) != 0 )
97  goto err_parse;
98 
99  /* Decide whether or not to download images */
101  download = ( ! ( kernel && efi_can_load ( kernel ) ) );
102 
103  /* Parse name/URI string */
104  name_uri = argv[optind];
105 
106  /* Acquire image, if applicable */
107  if ( download && name_uri &&
108  ( ( rc = imgacquire ( name_uri, opts.timeout,
109  &image ) ) != 0 ) ) {
110  goto err_image;
111  }
112 
113  /* (Un)register as shim */
114  if ( ( rc = shim ( image, opts.require_loader, opts.allow_pxe,
115  opts.allow_sbat ) ) != 0 )
116  goto err_shim;
117 
118  err_shim:
119  err_image:
120  err_parse:
121  err_dummy:
122  return rc;
123 }
124 
125 /** Shim commands */
126 struct command shim_commands[] __command = {
127  {
128  .name = "shim",
129  .exec = shim_exec,
130  },
131 };
struct image_tag selected_image
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int optind
Current option index.
Definition: getopt.c:51
EFI shim management.
static int shim_exec(int argc, char **argv)
The "shim" command.
Definition: shim_cmd.c:81
A command-line command.
Definition: command.h:9
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:114
static struct command_descriptor shim_cmd
"shim" command descriptor
Definition: shim_cmd.c:71
"shim" options
Definition: shim_cmd.c:47
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
An executable image.
Definition: image.h:24
A command descriptor.
Definition: parseopt.h:77
struct image * find_image_tag(struct image_tag *tag)
Find image by tag.
Definition: image.c:338
unsigned long timeout
Download timeout.
Definition: shim_cmd.c:49
Parse command-line options.
static struct option_descriptor shim_opts[]
"shim" option list
Definition: shim_cmd.c:59
uint32_t kernel
Kernel version (numeric)
Definition: ena.h:20
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:226
static int efi_can_load(struct image *image)
Check if EFI image can be loaded directly.
Definition: efi_image.h:22
Command line option parsing.
Option does not take an argument.
Definition: getopt.h:16
int require_loader
Require third party loader.
Definition: shim_cmd.c:51
static union @437 opts
"cert<xxx>" option list
const char * name
Name of the command.
Definition: command.h:11
Image management.
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:67
Option requires an argument.
Definition: getopt.h:18
EFI images.
A command-line option descriptor.
Definition: parseopt.h:23
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
void timeout(int)
#define shim_dummy
Definition: shim_cmd.c:43
int shim(struct image *image, int require_loader, int allow_pxe, int allow_sbat)
Set shim image.
Definition: shimmgmt.c:45
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
struct command shim_commands [] __command
Shim commands.
Definition: shim_cmd.c:126
int allow_pxe
Allow PXE base code protocol.
Definition: shim_cmd.c:53
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:141
int allow_sbat
Allow SBAT variable access.
Definition: shim_cmd.c:55