From b2501dd1221d7910e7aa896ae8b927bb63b77d3a Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Fri, 23 Apr 2021 12:27:57 +0100 Subject: [readline] Add an optional timeout to readline_history() Signed-off-by: Michael Brown --- src/hci/commands/nvo_cmd.c | 2 +- src/hci/readline.c | 17 ++++++++++++++--- src/hci/shell.c | 2 +- 3 files changed, 16 insertions(+), 5 deletions(-) (limited to 'src/hci') diff --git a/src/hci/commands/nvo_cmd.c b/src/hci/commands/nvo_cmd.c index ac0d60651..88e9d4714 100644 --- a/src/hci/commands/nvo_cmd.c +++ b/src/hci/commands/nvo_cmd.c @@ -241,7 +241,7 @@ static int read_value ( struct named_setting *setting, char **args __unused, NULL, &setting->setting, &existing ); /* Read new value */ - if ( ( rc = readline_history ( NULL, existing, NULL, value ) ) != 0 ) + if ( ( rc = readline_history ( NULL, existing, NULL, 0, value ) ) != 0 ) goto err_readline; err_readline: diff --git a/src/hci/readline.c b/src/hci/readline.c index 83a2e0b90..852c4503a 100644 --- a/src/hci/readline.c +++ b/src/hci/readline.c @@ -248,6 +248,7 @@ void history_free ( struct readline_history *history ) { * @v prompt Prompt string * @v prefill Prefill string, or NULL for no prefill * @v history History buffer, or NULL for no history + * @v timeout Timeout period, in ticks (0=indefinite) * @ret line Line read from console (excluding terminating newline) * @ret rc Return status code * @@ -255,7 +256,8 @@ void history_free ( struct readline_history *history ) { * eventually call free() to release the storage. */ int readline_history ( const char *prompt, const char *prefill, - struct readline_history *history, char **line ) { + struct readline_history *history, unsigned long timeout, + char **line ) { char buf[READLINE_MAX]; struct edit_string string; int key; @@ -285,8 +287,17 @@ int readline_history ( const char *prompt, const char *prefill, } while ( 1 ) { + + /* Get keypress */ + key = getkey ( timeout ); + if ( key < 0 ) { + rc = -ETIMEDOUT; + goto done; + } + timeout = 0; + /* Handle keypress */ - key = edit_string ( &string, getkey ( 0 ) ); + key = edit_string ( &string, key ); sync_console ( &string ); move_by = 0; switch ( key ) { @@ -342,6 +353,6 @@ int readline_history ( const char *prompt, const char *prefill, char * readline ( const char *prompt ) { char *line; - readline_history ( prompt, NULL, NULL, &line ); + readline_history ( prompt, NULL, NULL, 0, &line ); return line; } diff --git a/src/hci/shell.c b/src/hci/shell.c index 276eb3527..8ecf73a6f 100644 --- a/src/hci/shell.c +++ b/src/hci/shell.c @@ -91,7 +91,7 @@ int shell ( void ) { /* Read and execute commands */ do { - readline_history ( shell_prompt, NULL, &history, &line ); + readline_history ( shell_prompt, NULL, &history, 0, &line ); if ( line ) { rc = system ( line ); free ( line ); -- cgit v1.2.3-55-g7522 From 323af9ee84ae767f1699657d885bb4ca801c00ce Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Fri, 23 Apr 2021 12:30:30 +0100 Subject: [settings] Add --timeout option to "read" command Requested-by: Matteo Guglielmi Signed-off-by: Michael Brown --- src/hci/commands/nvo_cmd.c | 57 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 14 deletions(-) (limited to 'src/hci') diff --git a/src/hci/commands/nvo_cmd.c b/src/hci/commands/nvo_cmd.c index 88e9d4714..6ad7e7428 100644 --- a/src/hci/commands/nvo_cmd.c +++ b/src/hci/commands/nvo_cmd.c @@ -100,20 +100,40 @@ static int show_exec ( int argc, char **argv ) { } /** "set", "clear", and "read" options */ -struct set_core_options {}; +struct set_core_options { + /** Timeout */ + unsigned long timeout; +}; /** "set", "clear", and "read" option list */ -static struct option_descriptor set_core_opts[] = {}; +static union { + /* "set" takes no options */ + struct option_descriptor set[0]; + /* "clear" takes no options */ + struct option_descriptor clear[0]; + /* "read" takes --timeout option */ + struct option_descriptor read[1]; +} set_core_opts = { + .read = { + OPTION_DESC ( "timeout", 't', required_argument, + struct set_core_options, timeout, parse_timeout ), + }, +}; /** "set" command descriptor */ static struct command_descriptor set_cmd = - COMMAND_DESC ( struct set_core_options, set_core_opts, 1, MAX_ARGUMENTS, - " " ); + COMMAND_DESC ( struct set_core_options, set_core_opts.set, + 1, MAX_ARGUMENTS, " " ); + +/** "clear" command descriptor */ +static struct command_descriptor clear_cmd = + COMMAND_DESC ( struct set_core_options, set_core_opts.clear, + 1, 1, "" ); -/** "clear" and "read" command descriptor */ -static struct command_descriptor clear_read_cmd = - COMMAND_DESC ( struct set_core_options, set_core_opts, 1, 1, - "" ); +/** "read" command descriptor */ +static struct command_descriptor read_cmd = + COMMAND_DESC ( struct set_core_options, set_core_opts.read, + 1, 1, "" ); /** * "set", "clear", and "read" command @@ -127,6 +147,7 @@ static struct command_descriptor clear_read_cmd = static int set_core_exec ( int argc, char **argv, struct command_descriptor *cmd, int ( * get_value ) ( struct named_setting *setting, + struct set_core_options *opts, char **args, char **value ) ) { struct set_core_options opts; struct named_setting setting; @@ -143,7 +164,8 @@ static int set_core_exec ( int argc, char **argv, goto err_parse_setting; /* Parse setting value */ - if ( ( rc = get_value ( &setting, &argv[ optind + 1 ], &value ) ) != 0 ) + if ( ( rc = get_value ( &setting, &opts, &argv[ optind + 1 ], + &value ) ) != 0 ) goto err_get_value; /* Apply default type if necessary */ @@ -170,11 +192,13 @@ static int set_core_exec ( int argc, char **argv, * Get setting value for "set" command * * @v setting Named setting + * @v opts Options list * @v args Remaining arguments * @ret value Setting value * @ret rc Return status code */ static int set_value ( struct named_setting *setting __unused, + struct set_core_options *opts __unused, char **args, char **value ) { *value = concat_args ( args ); @@ -200,10 +224,12 @@ static int set_exec ( int argc, char **argv ) { * * @v setting Named setting * @v args Remaining arguments + * @v opts Options list * @ret value Setting value * @ret rc Return status code */ static int clear_value ( struct named_setting *setting __unused, + struct set_core_options *opts __unused, char **args __unused, char **value ) { *value = NULL; @@ -218,7 +244,7 @@ static int clear_value ( struct named_setting *setting __unused, * @ret rc Return status code */ static int clear_exec ( int argc, char **argv ) { - return set_core_exec ( argc, argv, &clear_read_cmd, clear_value ); + return set_core_exec ( argc, argv, &clear_cmd, clear_value ); } /** @@ -226,11 +252,13 @@ static int clear_exec ( int argc, char **argv ) { * * @v setting Named setting * @v args Remaining arguments + * @v opts Options list * @ret value Setting value * @ret rc Return status code */ -static int read_value ( struct named_setting *setting, char **args __unused, - char **value ) { +static int read_value ( struct named_setting *setting, + struct set_core_options *opts, + char **args __unused, char **value ) { char *existing; int rc; @@ -241,7 +269,8 @@ static int read_value ( struct named_setting *setting, char **args __unused, NULL, &setting->setting, &existing ); /* Read new value */ - if ( ( rc = readline_history ( NULL, existing, NULL, 0, value ) ) != 0 ) + if ( ( rc = readline_history ( NULL, existing, NULL, opts->timeout, + value ) ) != 0 ) goto err_readline; err_readline: @@ -257,7 +286,7 @@ static int read_value ( struct named_setting *setting, char **args __unused, * @ret rc Return status code */ static int read_exec ( int argc, char **argv ) { - return set_core_exec ( argc, argv, &clear_read_cmd, read_value ); + return set_core_exec ( argc, argv, &read_cmd, read_value ); } /** "inc" options */ -- cgit v1.2.3-55-g7522 From 5c9c8d2b9b78cf4e1f256fe6874855c1aee458f2 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Thu, 6 May 2021 13:11:31 +0100 Subject: [image] Add "imgextract" command for extracting archive images Add the concept of extracting an image from an archive (which could be a single-file archive such as a gzip-compressed file), along with an "imgextract" command to expose this functionality to scripts. Signed-off-by: Michael Brown --- src/config/config.c | 3 + src/config/config_archive.c | 32 +++++++++++ src/config/general.h | 1 + src/core/archive.c | 104 ++++++++++++++++++++++++++++++++++ src/hci/commands/image_archive_cmd.c | 105 +++++++++++++++++++++++++++++++++++ src/include/ipxe/errfile.h | 1 + src/include/ipxe/image.h | 10 ++++ src/include/usr/imgarchive.h | 16 ++++++ src/usr/imgarchive.c | 54 ++++++++++++++++++ 9 files changed, 326 insertions(+) create mode 100644 src/config/config_archive.c create mode 100644 src/core/archive.c create mode 100644 src/hci/commands/image_archive_cmd.c create mode 100644 src/include/usr/imgarchive.h create mode 100644 src/usr/imgarchive.c (limited to 'src/hci') diff --git a/src/config/config.c b/src/config/config.c index 5e7a3ecfd..dd1fceb92 100644 --- a/src/config/config.c +++ b/src/config/config.c @@ -284,6 +284,9 @@ REQUIRE_OBJECT ( cert_cmd ); #ifdef IMAGE_MEM_CMD REQUIRE_OBJECT ( image_mem_cmd ); #endif +#ifdef IMAGE_ARCHIVE_CMD +REQUIRE_OBJECT ( image_archive_cmd ); +#endif /* * Drag in miscellaneous objects diff --git a/src/config/config_archive.c b/src/config/config_archive.c new file mode 100644 index 000000000..3644b5de8 --- /dev/null +++ b/src/config/config_archive.c @@ -0,0 +1,32 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + * + * You can also choose to distribute this program under the terms of + * the Unmodified Binary Distribution Licence (as given in the file + * COPYING.UBDL), provided that you have satisfied its requirements. + */ + +FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); + +#include + +/** @file + * + * Archive image configuration + * + */ + +PROVIDE_REQUIRING_SYMBOL(); diff --git a/src/config/general.h b/src/config/general.h index 9b21f1271..fd317be96 100644 --- a/src/config/general.h +++ b/src/config/general.h @@ -156,6 +156,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); //#define NTP_CMD /* NTP commands */ //#define CERT_CMD /* Certificate management commands */ //#define IMAGE_MEM_CMD /* Read memory command */ +//#define IMAGE_ARCHIVE_CMD /* Archive image management commands */ /* * ROM-specific options diff --git a/src/core/archive.c b/src/core/archive.c new file mode 100644 index 000000000..cd024bde3 --- /dev/null +++ b/src/core/archive.c @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2021 Michael Brown . + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + * + * You can also choose to distribute this program under the terms of + * the Unmodified Binary Distribution Licence (as given in the file + * COPYING.UBDL), provided that you have satisfied its requirements. + */ + +FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); + +#include +#include +#include + +/** @file + * + * Archive images + * + */ + +/** + * Extract archive image + * + * @v image Image + * @v name Extracted image name + * @v extracted Extracted image to fill in + * @ret rc Return status code + */ +int image_extract ( struct image *image, const char *name, + struct image **extracted ) { + char *dot; + int rc; + + /* Check that this image can be used to extract an archive image */ + if ( ! ( image->type && image->type->extract ) ) { + rc = -ENOTSUP; + goto err_unsupported; + } + + /* Allocate new image */ + *extracted = alloc_image ( image->uri ); + if ( ! *extracted ) { + rc = -ENOMEM; + goto err_alloc; + } + + /* Set image name */ + if ( ( rc = image_set_name ( *extracted, + ( name ? name : image->name ) ) ) != 0 ) { + goto err_set_name; + } + + /* Strip any archive or compression suffix from implicit name */ + if ( ( ! name ) && ( (*extracted)->name ) && + ( ( dot = strrchr ( (*extracted)->name, '.' ) ) != NULL ) ) { + *dot = '\0'; + } + + /* Try extracting archive image */ + if ( ( rc = image->type->extract ( image, *extracted ) ) != 0 ) { + DBGC ( image, "IMAGE %s could not extract image: %s\n", + image->name, strerror ( rc ) ); + goto err_extract; + } + + /* Register image */ + if ( ( rc = register_image ( *extracted ) ) != 0 ) + goto err_register; + + /* Drop local reference to image */ + image_put ( *extracted ); + + return 0; + + unregister_image ( *extracted ); + err_register: + err_extract: + err_set_name: + image_put ( *extracted ); + err_alloc: + err_unsupported: + return rc; +} + +/* Drag in objects via image_extract() */ +REQUIRING_SYMBOL ( image_extract ); + +/* Drag in archive image formats */ +REQUIRE_OBJECT ( config_archive ); diff --git a/src/hci/commands/image_archive_cmd.c b/src/hci/commands/image_archive_cmd.c new file mode 100644 index 000000000..a2212aecf --- /dev/null +++ b/src/hci/commands/image_archive_cmd.c @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2021 Michael Brown . + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + * + * You can also choose to distribute this program under the terms of + * the Unmodified Binary Distribution Licence (as given in the file + * COPYING.UBDL), provided that you have satisfied its requirements. + */ + +FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); + +#include +#include +#include +#include +#include + +/** @file + * + * Archive image commands + * + */ + +/** "imgextract" options */ +struct imgextract_options { + /** Image name */ + char *name; + /** Keep original image */ + int keep; + /** Download timeout */ + unsigned long timeout; +}; + +/** "imgextract" option list */ +static struct option_descriptor imgextract_opts[] = { + OPTION_DESC ( "name", 'n', required_argument, + struct imgextract_options, name, parse_string ), + OPTION_DESC ( "keep", 'k', no_argument, + struct imgextract_options, keep, parse_flag ), + OPTION_DESC ( "timeout", 't', required_argument, + struct imgextract_options, timeout, parse_timeout ), +}; + +/** "imgextract" command descriptor */ +static struct command_descriptor imgextract_cmd = + COMMAND_DESC ( struct imgextract_options, imgextract_opts, 1, 1, NULL ); + +/** + * The "imgextract" command + * + * @v argc Argument count + * @v argv Argument list + * @ret rc Return status code + */ +static int imgextract_exec ( int argc, char **argv ) { + struct imgextract_options opts; + struct image *image; + int rc; + + /* Parse options */ + if ( ( rc = parse_options ( argc, argv, &imgextract_cmd, + &opts ) ) != 0 ) + goto err_parse; + + /* Acquire image */ + if ( ( rc = imgacquire ( argv[optind], opts.timeout, &image ) ) != 0 ) + goto err_acquire; + + /* Extract archive image */ + if ( ( rc = imgextract ( image, opts.name ) ) != 0 ) + goto err_extract; + + /* Success */ + rc = 0; + + err_extract: + /* Discard original image unless --keep was specified */ + if ( ! opts.keep ) + unregister_image ( image ); + err_acquire: + err_parse: + return rc; +} + +/** Archive image commands */ +struct command image_archive_commands[] __command = { + { + .name = "imgextract", + .exec = imgextract_exec, + }, +}; diff --git a/src/include/ipxe/errfile.h b/src/include/ipxe/errfile.h index e3bf9f565..162e6b7b0 100644 --- a/src/include/ipxe/errfile.h +++ b/src/include/ipxe/errfile.h @@ -301,6 +301,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); #define ERRFILE_png ( ERRFILE_IMAGE | 0x00070000 ) #define ERRFILE_der ( ERRFILE_IMAGE | 0x00080000 ) #define ERRFILE_pem ( ERRFILE_IMAGE | 0x00090000 ) +#define ERRFILE_archive ( ERRFILE_IMAGE | 0x000a0000 ) #define ERRFILE_asn1 ( ERRFILE_OTHER | 0x00000000 ) #define ERRFILE_chap ( ERRFILE_OTHER | 0x00010000 ) diff --git a/src/include/ipxe/image.h b/src/include/ipxe/image.h index 046edf9a5..c6e723dc6 100644 --- a/src/include/ipxe/image.h +++ b/src/include/ipxe/image.h @@ -113,6 +113,14 @@ struct image_type { */ int ( * asn1 ) ( struct image *image, size_t offset, struct asn1_cursor **cursor ); + /** + * Extract archive image + * + * @v image Image + * @v extracted Extracted image + * @ret rc Return status code + */ + int ( * extract ) ( struct image *image, struct image *extracted ); }; /** @@ -190,6 +198,8 @@ extern struct image * image_memory ( const char *name, userptr_t data, extern int image_pixbuf ( struct image *image, struct pixel_buffer **pixbuf ); extern int image_asn1 ( struct image *image, size_t offset, struct asn1_cursor **cursor ); +extern int image_extract ( struct image *image, const char *name, + struct image **extracted ); /** * Increment reference count on an image diff --git a/src/include/usr/imgarchive.h b/src/include/usr/imgarchive.h new file mode 100644 index 000000000..bf0c18f55 --- /dev/null +++ b/src/include/usr/imgarchive.h @@ -0,0 +1,16 @@ +#ifndef _USR_IMGARCHIVE_H +#define _USR_IMGARCHIVE_H + +/** @file + * + * Archive image management + * + */ + +FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); + +#include + +extern int imgextract ( struct image *image, const char *name ); + +#endif /* _USR_IMGARCHIVE_H */ diff --git a/src/usr/imgarchive.c b/src/usr/imgarchive.c new file mode 100644 index 000000000..6849dd510 --- /dev/null +++ b/src/usr/imgarchive.c @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2021 Michael Brown . + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + * + * You can also choose to distribute this program under the terms of + * the Unmodified Binary Distribution Licence (as given in the file + * COPYING.UBDL), provided that you have satisfied its requirements. + */ + +FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); + +#include +#include +#include + +/** @file + * + * Archive image management + * + */ + +/** + * Extract archive image + * + * @v image Image + * @v name Extracted image name (or NULL to use default) + * @ret rc Return status code + */ +int imgextract ( struct image *image, const char *name ) { + struct image *extracted; + int rc; + + /* Extract archive image */ + if ( ( rc = image_extract ( image, name, &extracted ) ) != 0 ) { + printf ( "Could not extract image: %s\n", strerror ( rc ) ); + return rc; + } + + return 0; +} -- cgit v1.2.3-55-g7522