mirror of
https://github.com/ipxe/ipxe
synced 2026-02-28 03:11:18 +03:00
[image] Generalise "currently-running script" to "currently-running image"
Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
@@ -39,6 +39,9 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
|||||||
/** List of registered images */
|
/** List of registered images */
|
||||||
struct list_head images = LIST_HEAD_INIT ( images );
|
struct list_head images = LIST_HEAD_INIT ( images );
|
||||||
|
|
||||||
|
/** Currently-executing image */
|
||||||
|
struct image *current_image;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Free executable image
|
* Free executable image
|
||||||
*
|
*
|
||||||
@@ -200,6 +203,7 @@ int image_probe ( struct image *image ) {
|
|||||||
* @ret rc Return status code
|
* @ret rc Return status code
|
||||||
*/
|
*/
|
||||||
int image_exec ( struct image *image ) {
|
int image_exec ( struct image *image ) {
|
||||||
|
struct image *saved_current_image;
|
||||||
struct image *replacement;
|
struct image *replacement;
|
||||||
struct uri *old_cwuri;
|
struct uri *old_cwuri;
|
||||||
int rc;
|
int rc;
|
||||||
@@ -212,11 +216,14 @@ int image_exec ( struct image *image ) {
|
|||||||
old_cwuri = uri_get ( cwuri );
|
old_cwuri = uri_get ( cwuri );
|
||||||
churi ( image->uri );
|
churi ( image->uri );
|
||||||
|
|
||||||
|
/* Preserve record of any currently-running image */
|
||||||
|
saved_current_image = current_image;
|
||||||
|
|
||||||
/* Take out a temporary reference to the image. This allows
|
/* Take out a temporary reference to the image. This allows
|
||||||
* the image to unregister itself if necessary, without
|
* the image to unregister itself if necessary, without
|
||||||
* automatically freeing itself.
|
* automatically freeing itself.
|
||||||
*/
|
*/
|
||||||
image_get ( image );
|
current_image = image_get ( image );
|
||||||
|
|
||||||
/* Try executing the image */
|
/* Try executing the image */
|
||||||
if ( ( rc = image->type->exec ( image ) ) != 0 ) {
|
if ( ( rc = image->type->exec ( image ) ) != 0 ) {
|
||||||
@@ -233,6 +240,9 @@ int image_exec ( struct image *image ) {
|
|||||||
/* Drop temporary reference to the original image */
|
/* Drop temporary reference to the original image */
|
||||||
image_put ( image );
|
image_put ( image );
|
||||||
|
|
||||||
|
/* Restore previous currently-running image */
|
||||||
|
current_image = saved_current_image;
|
||||||
|
|
||||||
/* Reset current working directory */
|
/* Reset current working directory */
|
||||||
churi ( old_cwuri );
|
churi ( old_cwuri );
|
||||||
uri_put ( old_cwuri );
|
uri_put ( old_cwuri );
|
||||||
|
|||||||
@@ -38,13 +38,6 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
|||||||
#include <usr/prompt.h>
|
#include <usr/prompt.h>
|
||||||
#include <ipxe/script.h>
|
#include <ipxe/script.h>
|
||||||
|
|
||||||
/** Currently running script
|
|
||||||
*
|
|
||||||
* This is a global in order to allow goto_exec() to update the
|
|
||||||
* offset.
|
|
||||||
*/
|
|
||||||
static struct image *script;
|
|
||||||
|
|
||||||
/** Offset within current script
|
/** Offset within current script
|
||||||
*
|
*
|
||||||
* This is a global in order to allow goto_exec() to update the
|
* This is a global in order to allow goto_exec() to update the
|
||||||
@@ -55,11 +48,13 @@ static size_t script_offset;
|
|||||||
/**
|
/**
|
||||||
* Process script lines
|
* Process script lines
|
||||||
*
|
*
|
||||||
|
* @v image Script
|
||||||
* @v process_line Line processor
|
* @v process_line Line processor
|
||||||
* @v terminate Termination check
|
* @v terminate Termination check
|
||||||
* @ret rc Return status code
|
* @ret rc Return status code
|
||||||
*/
|
*/
|
||||||
static int process_script ( int ( * process_line ) ( const char *line ),
|
static int process_script ( struct image *image,
|
||||||
|
int ( * process_line ) ( const char *line ),
|
||||||
int ( * terminate ) ( int rc ) ) {
|
int ( * terminate ) ( int rc ) ) {
|
||||||
off_t eol;
|
off_t eol;
|
||||||
size_t len;
|
size_t len;
|
||||||
@@ -70,17 +65,17 @@ static int process_script ( int ( * process_line ) ( const char *line ),
|
|||||||
do {
|
do {
|
||||||
|
|
||||||
/* Find length of next line, excluding any terminating '\n' */
|
/* Find length of next line, excluding any terminating '\n' */
|
||||||
eol = memchr_user ( script->data, script_offset, '\n',
|
eol = memchr_user ( image->data, script_offset, '\n',
|
||||||
( script->len - script_offset ) );
|
( image->len - script_offset ) );
|
||||||
if ( eol < 0 )
|
if ( eol < 0 )
|
||||||
eol = script->len;
|
eol = image->len;
|
||||||
len = ( eol - script_offset );
|
len = ( eol - script_offset );
|
||||||
|
|
||||||
/* Copy line, terminate with NUL, and execute command */
|
/* Copy line, terminate with NUL, and execute command */
|
||||||
{
|
{
|
||||||
char cmdbuf[ len + 1 ];
|
char cmdbuf[ len + 1 ];
|
||||||
|
|
||||||
copy_from_user ( cmdbuf, script->data,
|
copy_from_user ( cmdbuf, image->data,
|
||||||
script_offset, len );
|
script_offset, len );
|
||||||
cmdbuf[len] = '\0';
|
cmdbuf[len] = '\0';
|
||||||
DBG ( "$ %s\n", cmdbuf );
|
DBG ( "$ %s\n", cmdbuf );
|
||||||
@@ -94,7 +89,7 @@ static int process_script ( int ( * process_line ) ( const char *line ),
|
|||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
} while ( script_offset < script->len );
|
} while ( script_offset < image->len );
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
@@ -138,7 +133,6 @@ static int script_exec_line ( const char *line ) {
|
|||||||
* @ret rc Return status code
|
* @ret rc Return status code
|
||||||
*/
|
*/
|
||||||
static int script_exec ( struct image *image ) {
|
static int script_exec ( struct image *image ) {
|
||||||
struct image *saved_script;
|
|
||||||
size_t saved_offset;
|
size_t saved_offset;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
@@ -148,18 +142,14 @@ static int script_exec ( struct image *image ) {
|
|||||||
unregister_image ( image );
|
unregister_image ( image );
|
||||||
|
|
||||||
/* Preserve state of any currently-running script */
|
/* Preserve state of any currently-running script */
|
||||||
saved_script = script;
|
|
||||||
saved_offset = script_offset;
|
saved_offset = script_offset;
|
||||||
|
|
||||||
/* Initialise state for this script */
|
|
||||||
script = image;
|
|
||||||
|
|
||||||
/* Process script */
|
/* Process script */
|
||||||
rc = process_script ( script_exec_line, terminate_on_exit_or_failure );
|
rc = process_script ( image, script_exec_line,
|
||||||
|
terminate_on_exit_or_failure );
|
||||||
|
|
||||||
/* Restore saved state, re-register image, and return */
|
/* Restore saved state, re-register image, and return */
|
||||||
script_offset = saved_offset;
|
script_offset = saved_offset;
|
||||||
script = saved_script;
|
|
||||||
register_image ( image );
|
register_image ( image );
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
@@ -262,7 +252,7 @@ static int goto_exec ( int argc, char **argv ) {
|
|||||||
return rc;
|
return rc;
|
||||||
|
|
||||||
/* Sanity check */
|
/* Sanity check */
|
||||||
if ( ! script ) {
|
if ( ! current_image ) {
|
||||||
rc = -ENOTTY;
|
rc = -ENOTTY;
|
||||||
printf ( "Not in a script: %s\n", strerror ( rc ) );
|
printf ( "Not in a script: %s\n", strerror ( rc ) );
|
||||||
return rc;
|
return rc;
|
||||||
@@ -273,7 +263,7 @@ static int goto_exec ( int argc, char **argv ) {
|
|||||||
|
|
||||||
/* Find label */
|
/* Find label */
|
||||||
saved_offset = script_offset;
|
saved_offset = script_offset;
|
||||||
if ( ( rc = process_script ( goto_find_label,
|
if ( ( rc = process_script ( current_image, goto_find_label,
|
||||||
terminate_on_label_found ) ) != 0 ) {
|
terminate_on_label_found ) ) != 0 ) {
|
||||||
script_offset = saved_offset;
|
script_offset = saved_offset;
|
||||||
return rc;
|
return rc;
|
||||||
|
|||||||
@@ -110,6 +110,7 @@ struct image_type {
|
|||||||
#define __image_type( probe_order ) __table_entry ( IMAGE_TYPES, probe_order )
|
#define __image_type( probe_order ) __table_entry ( IMAGE_TYPES, probe_order )
|
||||||
|
|
||||||
extern struct list_head images;
|
extern struct list_head images;
|
||||||
|
extern struct image *current_image;
|
||||||
|
|
||||||
/** Iterate over all registered images */
|
/** Iterate over all registered images */
|
||||||
#define for_each_image( image ) \
|
#define for_each_image( image ) \
|
||||||
|
|||||||
Reference in New Issue
Block a user