diff --git a/src/include/ipxe/efi/efi_strings.h b/src/include/ipxe/efi/efi_strings.h new file mode 100644 index 00000000..023ccda0 --- /dev/null +++ b/src/include/ipxe/efi/efi_strings.h @@ -0,0 +1,23 @@ +#ifndef _IPXE_EFI_STRINGS_H +#define _IPXE_EFI_STRINGS_H + +/** @file + * + * EFI strings + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +#include +#include +#include + +extern int efi_vsnprintf ( wchar_t *wbuf, size_t wsize, const char *fmt, + va_list args ); +extern int efi_snprintf ( wchar_t *wbuf, size_t wsize, const char *fmt, ... ); +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, ... ); + +#endif /* _IPXE_EFI_STRINGS_H */ diff --git a/src/interface/efi/efi_driver.c b/src/interface/efi/efi_driver.c index 17ec881d..4aa976f9 100644 --- a/src/interface/efi/efi_driver.c +++ b/src/interface/efi/efi_driver.c @@ -23,6 +23,7 @@ FILE_LICENCE ( GPL2_OR_LATER ); #include #include #include +#include #include #include @@ -107,8 +108,6 @@ EFI_STATUS efi_driver_install ( struct efi_driver *efidrv ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; EFI_DRIVER_BINDING_PROTOCOL *driver = &efidrv->driver; EFI_COMPONENT_NAME2_PROTOCOL *wtf = &efidrv->wtf; - char buf[ sizeof ( efidrv->wname ) / sizeof ( efidrv->wname[0] ) ]; - unsigned int i; EFI_STATUS efirc; /* Configure driver binding protocol */ @@ -120,12 +119,10 @@ EFI_STATUS efi_driver_install ( struct efi_driver *efidrv ) { wtf->SupportedLanguages = "en"; /* Fill in driver name */ - snprintf ( buf, sizeof ( buf ), PRODUCT_SHORT_NAME " - %s", - efidrv->name ); - for ( i = 0 ; i < sizeof ( buf ) ; i++ ) { - /* Damn Unicode names */ - efidrv->wname[i] = *( ( ( unsigned char * ) buf ) + i ); - } + efi_snprintf ( efidrv->wname, + ( sizeof ( efidrv->wname ) / + sizeof ( efidrv->wname[0] ) ), + PRODUCT_SHORT_NAME " - %s", efidrv->name ); /* Install driver */ if ( ( efirc = bs->InstallMultipleProtocolInterfaces ( diff --git a/src/interface/efi/efi_snp.c b/src/interface/efi/efi_snp.c index cc09d7db..608aaf50 100644 --- a/src/interface/efi/efi_snp.c +++ b/src/interface/efi/efi_snp.c @@ -30,6 +30,7 @@ FILE_LICENCE ( GPL2_OR_LATER ); #include #include #include +#include #include #include #include @@ -772,7 +773,6 @@ static int efi_snp_probe ( struct net_device *netdev ) { EFI_DEVICE_PATH_PROTOCOL *path_end; MAC_ADDR_DEVICE_PATH *macpath; size_t path_prefix_len = 0; - unsigned int i; EFI_STATUS efirc; int rc; @@ -831,12 +831,9 @@ static int efi_snp_probe ( struct net_device *netdev ) { sizeof ( snpdev->nii.StringId ) ); /* Populate the device name */ - for ( i = 0 ; i < sizeof ( netdev->name ) ; i++ ) { - /* Damn Unicode names */ - assert ( i < ( sizeof ( snpdev->name ) / - sizeof ( snpdev->name[0] ) ) ); - snpdev->name[i] = netdev->name[i]; - } + efi_snprintf ( snpdev->name, ( sizeof ( snpdev->name ) / + sizeof ( snpdev->name[0] ) ), + "%s", netdev->name ); /* Populate the device path */ memcpy ( &snpdev->path, efipci->path, path_prefix_len ); diff --git a/src/interface/efi/efi_strings.c b/src/interface/efi/efi_strings.c new file mode 100644 index 00000000..0fbc4538 --- /dev/null +++ b/src/interface/efi/efi_strings.c @@ -0,0 +1,147 @@ +/* + * Copyright (C) 2011 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., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +#include +#include +#include +#include + +/** Context used by efi_vsnprintf() and friends */ +struct efi_sputc_context { + /** printf context */ + struct printf_context ctx; + /** Buffer for formatted string (used by efi_printf_sputc()) */ + wchar_t *buf; + /** Buffer length (used by efi_printf_sputc()) + * + * Note that this is a number of wide characters, not a number + * of bytes. + */ + size_t max_wlen; +}; + +/** + * Write wide character to buffer + * + * @v ctx Context + * @v c Character + */ +static void efi_printf_sputc ( struct printf_context *ctx, unsigned int c ) { + struct efi_sputc_context * sctx = + container_of ( ctx, struct efi_sputc_context, ctx ); + + if ( ctx->len < sctx->max_wlen ) + sctx->buf[ctx->len] = c; +} + +/** + * Write a formatted string to a wide-character buffer + * + * @v wbuf Buffer into which to write the string + * @v wsize Size of buffer (in wide characters) + * @v fmt Format string + * @v args Arguments corresponding to the format string + * @ret wlen Length of formatted string (in wide characters) + * + * If the buffer is too small to contain the string, the returned + * length is the length that would have been written had enough space + * been available. + */ +int efi_vsnprintf ( wchar_t *wbuf, size_t wsize, const char *fmt, + va_list args ) { + struct efi_sputc_context sctx; + size_t wlen; + size_t wend; + + /* Hand off to vcprintf */ + sctx.ctx.handler = efi_printf_sputc; + sctx.buf = wbuf; + sctx.max_wlen = wsize; + wlen = vcprintf ( &sctx.ctx, fmt, args ); + + /* Add trailing NUL */ + if ( wsize ) { + wend = wsize - 1; + if ( wlen < wend ) + wend = wlen; + wbuf[wend] = '\0'; + } + + return wlen; +} + +/** + * Write a formatted string to a buffer + * + * @v wbuf Buffer into which to write the string + * @v wsize Size of buffer (in wide characters) + * @v fmt Format string + * @v ... Arguments corresponding to the format string + * @ret wlen Length of formatted string (in wide characters) + */ +int efi_snprintf ( wchar_t *wbuf, size_t wsize, const char *fmt, ... ) { + va_list args; + int i; + + va_start ( args, fmt ); + i = efi_vsnprintf ( wbuf, wsize, fmt, args ); + va_end ( args ); + return i; +} + +/** + * Version of efi_vsnprintf() that accepts a signed buffer size + * + * @v wbuf Buffer into which to write the string + * @v swsize Size of buffer (in wide characters) + * @v fmt Format string + * @v args Arguments corresponding to the format string + * @ret wlen Length of formatted string (in wide characters) + */ +int efi_vssnprintf ( wchar_t *wbuf, ssize_t swsize, const char *fmt, + va_list args ) { + + /* Treat negative buffer size as zero buffer size */ + if ( swsize < 0 ) + swsize = 0; + + /* Hand off to vsnprintf */ + return efi_vsnprintf ( wbuf, swsize, fmt, args ); +} + +/** + * Version of efi_vsnprintf() that accepts a signed buffer size + * + * @v wbuf Buffer into which to write the string + * @v swsize Size of buffer (in wide characters) + * @v fmt Format string + * @v ... Arguments corresponding to the format string + * @ret wlen Length of formatted string (in wide characters) + */ +int efi_ssnprintf ( wchar_t *wbuf, ssize_t swsize, const char *fmt, ... ) { + va_list args; + int len; + + /* Hand off to vssnprintf */ + va_start ( args, fmt ); + len = efi_vssnprintf ( wbuf, swsize, fmt, args ); + va_end ( args ); + return len; +}