]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[efi] Add efi_asprintf() and efi_vasprintf()
authorMichael Brown <mcb30@ipxe.org>
Mon, 22 May 2023 12:35:34 +0000 (13:35 +0100)
committerMichael Brown <mcb30@ipxe.org>
Mon, 22 May 2023 14:10:16 +0000 (15:10 +0100)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/image/efi_image.c
src/include/ipxe/efi/efi_strings.h
src/include/ipxe/errfile.h
src/interface/efi/efi_strings.c

index 6a7bba01488515dcc1b9bbcb89ef62216f1b2cd3..43713403566f961b6a33e26ae854fed2a446f6c8 100644 (file)
@@ -109,18 +109,14 @@ efi_image_path ( struct image *image, EFI_DEVICE_PATH_PROTOCOL *parent ) {
  */
 static wchar_t * efi_image_cmdline ( struct image *image ) {
        wchar_t *cmdline;
-       size_t len;
 
-       len = ( strlen ( image->name ) +
-               ( image->cmdline ?
-                 ( 1 /* " " */ + strlen ( image->cmdline ) ) : 0 ) );
-       cmdline = zalloc ( ( len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
-       if ( ! cmdline )
+       /* Allocate and construct command line */
+       if ( efi_asprintf ( &cmdline, "%s%s%s", image->name,
+                           ( image->cmdline ? " " : "" ),
+                           ( image->cmdline ? image->cmdline : "" ) ) < 0 ) {
                return NULL;
-       efi_snprintf ( cmdline, ( len + 1 /* NUL */ ), "%s%s%s",
-                      image->name,
-                      ( image->cmdline ? " " : "" ),
-                      ( image->cmdline ? image->cmdline : "" ) );
+       }
+
        return cmdline;
 }
 
index a8ace45e740daeb15afe017570656a9a1a46a342..a7adff8277a55e058b1fcff33897b44f25e90be4 100644 (file)
@@ -19,6 +19,8 @@ extern int efi_vssnprintf ( wchar_t *wbuf, ssize_t swsize, const char *fmt,
                            va_list args );
 extern int efi_ssnprintf ( wchar_t *wbuf, ssize_t swsize,
                           const char *fmt, ... );
+extern int efi_vasprintf ( wchar_t **strp, const char *fmt, va_list args );
+extern int efi_asprintf ( wchar_t **strp, const char *fmt, ... );
 
 /**
  * Write a formatted string to a wide-character buffer
index e6fd8524e154e365780ff6cca5a93427c18a035b..235611a51adeabb7d8185ac2486215b036e34ca5 100644 (file)
@@ -78,6 +78,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 #define ERRFILE_dma                   ( ERRFILE_CORE | 0x00260000 )
 #define ERRFILE_cachedhcp             ( ERRFILE_CORE | 0x00270000 )
 #define ERRFILE_acpimac                       ( ERRFILE_CORE | 0x00280000 )
+#define ERRFILE_efi_strings           ( ERRFILE_CORE | 0x00290000 )
 
 #define ERRFILE_eisa                ( ERRFILE_DRIVER | 0x00000000 )
 #define ERRFILE_isa                 ( ERRFILE_DRIVER | 0x00010000 )
index aa3afc64f60c424c6213e57c64e25a4b4e7901bb..765b23ca63b5d088178eae8c35cd656e2c6978de 100644 (file)
@@ -25,6 +25,8 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 
 #include <stddef.h>
 #include <stdarg.h>
+#include <stdlib.h>
+#include <errno.h>
 #include <ipxe/vsprintf.h>
 #include <ipxe/efi/efi_strings.h>
 
@@ -150,3 +152,45 @@ int efi_ssnprintf ( wchar_t *wbuf, ssize_t swsize, const char *fmt, ... ) {
        va_end ( args );
        return len;
 }
+
+/**
+ * Write a formatted string to newly allocated memory
+ *
+ * @v wstrp            Pointer to hold allocated string
+ * @v fmt              Format string
+ * @v args             Arguments corresponding to the format string
+ * @ret len            Length of formatted string (in wide characters)
+ */
+int efi_vasprintf ( wchar_t **wstrp, const char *fmt, va_list args ) {
+       size_t len;
+       va_list args_tmp;
+
+       /* Calculate length needed for string */
+       va_copy ( args_tmp, args );
+       len = ( efi_vsnprintf ( NULL, 0, fmt, args_tmp ) + 1 );
+       va_end ( args_tmp );
+
+       /* Allocate and fill string */
+       *wstrp = malloc ( len * sizeof ( **wstrp ) );
+       if ( ! *wstrp )
+               return -ENOMEM;
+       return efi_vsnprintf ( *wstrp, len, fmt, args );
+}
+
+/**
+ * Write a formatted string to newly allocated memory
+ *
+ * @v wstrp            Pointer to hold allocated string
+ * @v fmt              Format string
+ * @v ...              Arguments corresponding to the format string
+ * @ret len            Length of formatted string (in wide characters)
+ */
+int efi_asprintf ( wchar_t **wstrp, const char *fmt, ... ) {
+       va_list args;
+       int len;
+
+       va_start ( args, fmt );
+       len = efi_vasprintf ( wstrp, fmt, args );
+       va_end ( args );
+       return len;
+}