blob: 8bf1e493b89e0f355616609fe45379b4f3432765 [file] [log] [blame]
AKASHI Takahiro94074682020-10-29 13:47:46 +09001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * String functions
4 *
5 * Copyright (c) 2020 AKASHI Takahiro, Linaro Limited
6 */
7
8#include <common.h>
9#include <charset.h>
Heinrich Schuchardt476ff2c2021-04-21 12:39:15 +020010#include <efi_loader.h>
AKASHI Takahiro94074682020-10-29 13:47:46 +090011
12/**
13 * efi_create_indexed_name - create a string name with an index
14 * @buffer: Buffer
15 * @name: Name string
16 * @index: Index
17 *
18 * Create a utf-16 string with @name, appending @index.
Simon Glass90975372022-01-23 12:55:12 -070019 * For example, u"Capsule0001"
AKASHI Takahiro94074682020-10-29 13:47:46 +090020 *
21 * The caller must ensure that the buffer has enough space for the resulting
22 * string including the trailing L'\0'.
23 *
24 * Return: A pointer to the next position after the created string
25 * in @buffer, or NULL otherwise
26 */
Ilias Apalodimas21575292020-12-31 12:26:46 +020027u16 *efi_create_indexed_name(u16 *buffer, size_t buffer_size, const char *name,
28 unsigned int index)
AKASHI Takahiro94074682020-10-29 13:47:46 +090029{
30 u16 *p = buffer;
31 char index_buf[5];
Ilias Apalodimas21575292020-12-31 12:26:46 +020032 size_t size;
AKASHI Takahiro94074682020-10-29 13:47:46 +090033
Ilias Apalodimas21575292020-12-31 12:26:46 +020034 size = (utf8_utf16_strlen(name) * sizeof(u16) +
35 sizeof(index_buf) * sizeof(u16));
36 if (buffer_size < size)
37 return NULL;
AKASHI Takahiro94074682020-10-29 13:47:46 +090038 utf8_utf16_strcpy(&p, name);
Ilias Apalodimas21575292020-12-31 12:26:46 +020039 snprintf(index_buf, sizeof(index_buf), "%04X", index);
AKASHI Takahiro94074682020-10-29 13:47:46 +090040 utf8_utf16_strcpy(&p, index_buf);
41
42 return p;
43}