Skip to content

Commit 1aba87f

Browse files
James Bottomleyardbiesheuvel
authored andcommitted
efivarfs: add helper to convert from UC16 name and GUID to utf8 name
These will be used by a later patch to check for uniqueness on initial EFI variable iteration. Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com> Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
1 parent c57b6e1 commit 1aba87f

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

fs/efivarfs/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ bool efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data,
5555
unsigned long data_size);
5656
bool efivar_variable_is_removable(efi_guid_t vendor, const char *name,
5757
size_t len);
58+
char *efivar_get_utf8name(const efi_char16_t *name16, efi_guid_t *vendor);
5859

5960
extern const struct file_operations efivarfs_file_operations;
6061
extern const struct inode_operations efivarfs_dir_inode_operations;

fs/efivarfs/super.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -205,27 +205,16 @@ static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
205205
memcpy(entry->var.VariableName, name16, name_size);
206206
memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
207207

208-
len = ucs2_utf8size(entry->var.VariableName);
209-
210-
/* name, plus '-', plus GUID, plus NUL*/
211-
name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
208+
name = efivar_get_utf8name(name16, &vendor);
212209
if (!name)
213210
goto fail;
214211

215-
ucs2_as_utf8(name, entry->var.VariableName, len);
212+
/* length of the variable name itself: remove GUID and separator */
213+
len = strlen(name) - EFI_VARIABLE_GUID_LEN - 1;
216214

217215
if (efivar_variable_is_removable(entry->var.VendorGuid, name, len))
218216
is_removable = true;
219217

220-
name[len] = '-';
221-
222-
efi_guid_to_str(&entry->var.VendorGuid, name + len + 1);
223-
224-
name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
225-
226-
/* replace invalid slashes like kobject_set_name_vargs does for /sys/firmware/efi/vars. */
227-
strreplace(name, '/', '!');
228-
229218
inode = efivarfs_get_inode(sb, d_inode(root), S_IFREG | 0644, 0,
230219
is_removable);
231220
if (!inode)

fs/efivarfs/vars.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,31 @@ variable_matches(const char *var_name, size_t len, const char *match_name,
225225
}
226226
}
227227

228+
char *
229+
efivar_get_utf8name(const efi_char16_t *name16, efi_guid_t *vendor)
230+
{
231+
int len = ucs2_utf8size(name16);
232+
char *name;
233+
234+
/* name, plus '-', plus GUID, plus NUL*/
235+
name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
236+
if (!name)
237+
return NULL;
238+
239+
ucs2_as_utf8(name, name16, len);
240+
241+
name[len] = '-';
242+
243+
efi_guid_to_str(vendor, name + len + 1);
244+
245+
name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
246+
247+
/* replace invalid slashes like kobject_set_name_vargs does for /sys/firmware/efi/vars. */
248+
strreplace(name, '/', '!');
249+
250+
return name;
251+
}
252+
228253
bool
229254
efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data,
230255
unsigned long data_size)

0 commit comments

Comments
 (0)