From: Alexander Graf Date: Wed, 14 Nov 2018 11:18:57 +0100 Subject: [U-Boot] [PATCH v13 1/4] sandbox: smbios: Update to support sandbox In-Reply-To: <20181114065043.241424-2-sjg@chromium.org> References: <20181114065043.241424-1-sjg@chromium.org> <20181114065043.241424-2-sjg@chromium.org> Message-ID: List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable To: u-boot@lists.denx.de On 11/14/2018 07:50 AM, Simon Glass wrote: > At present this code casts addresses to pointers so cannot be used with > sandbox. Update it to use mapmem instead. > > Signed-off-by: Simon Glass > --- > > Changes in v13: > - Update code to deal with the struct_table_address member > > Changes in v12: None > Changes in v11: > - Fix the EFI code that has since been added and relies on broken behavio= ur > > Changes in v9: None > Changes in v7: None > Changes in v5: None > Changes in v4: None > Changes in v3: > - Drop incorrect map_sysmem() in write_smbios_table() > > lib/efi_loader/efi_smbios.c | 20 +++++++++----- > lib/smbios.c | 52 ++++++++++++++++++++++++++++++------- > 2 files changed, 56 insertions(+), 16 deletions(-) > > diff --git a/lib/efi_loader/efi_smbios.c b/lib/efi_loader/efi_smbios.c > index 38e42fa2432..a81488495e2 100644 > --- a/lib/efi_loader/efi_smbios.c > +++ b/lib/efi_loader/efi_smbios.c > @@ -7,6 +7,7 @@ > =20 > #include > #include > +#include > #include > =20 > static const efi_guid_t smbios_guid =3D SMBIOS_TABLE_GUID; > @@ -19,17 +20,19 @@ static const efi_guid_t smbios_guid =3D SMBIOS_TABLE_= GUID; > efi_status_t efi_smbios_register(void) > { > /* Map within the low 32 bits, to allow for 32bit SMBIOS tables */ > - u64 dmi =3D U32_MAX; > + u64 dmi_addr =3D U32_MAX; > efi_status_t ret; > + void *dmi; > =20 > /* Reserve 4kiB page for SMBIOS */ > ret =3D efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, > - EFI_RUNTIME_SERVICES_DATA, 1, &dmi); > + EFI_RUNTIME_SERVICES_DATA, 1, &dmi_addr); > =20 > if (ret !=3D EFI_SUCCESS) { > /* Could not find space in lowmem, use highmem instead */ > ret =3D efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, > - EFI_RUNTIME_SERVICES_DATA, 1, &dmi); > + EFI_RUNTIME_SERVICES_DATA, 1, > + &dmi_addr); > =20 > if (ret !=3D EFI_SUCCESS) > return ret; > @@ -39,11 +42,14 @@ efi_status_t efi_smbios_register(void) > * Generate SMBIOS tables - we know that efi_allocate_pages() returns > * a 4k-aligned address, so it is safe to assume that > * write_smbios_table() will write the table at that address. > + * > + * Note that on sandbox, efi_allocate_pages() unfortunately returns a > + * pointer even though it uses a uint64_t type. Convert it. > */ > - assert(!(dmi & 0xf)); > - write_smbios_table(dmi); > + assert(!(dmi_addr & 0xf)); > + dmi =3D (void *)(uintptr_t)dmi_addr; > + write_smbios_table(map_to_sysmem(dmi)); > =20 > /* And expose them to our EFI payload */ > - return efi_install_configuration_table(&smbios_guid, > - (void *)(uintptr_t)dmi); > + return efi_install_configuration_table(&smbios_guid, dmi); > } > diff --git a/lib/smbios.c b/lib/smbios.c > index 326eb00230d..2cd08b5e16f 100644 > --- a/lib/smbios.c > +++ b/lib/smbios.c > @@ -6,6 +6,7 @@ > */ > =20 > #include > +#include > #include > #include > #include > @@ -72,9 +73,10 @@ static int smbios_string_table_len(char *start) > =20 > static int smbios_write_type0(ulong *current, int handle) > { > - struct smbios_type0 *t =3D (struct smbios_type0 *)*current; > + struct smbios_type0 *t; > int len =3D sizeof(struct smbios_type0); > =20 > + t =3D map_sysmem(*current, len); > memset(t, 0, sizeof(struct smbios_type0)); > fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle); > t->vendor =3D smbios_add_string(t->eos, "U-Boot"); > @@ -101,16 +103,18 @@ static int smbios_write_type0(ulong *current, int h= andle) > =20 > len =3D t->length + smbios_string_table_len(t->eos); > *current +=3D len; > + unmap_sysmem(t); > =20 > return len; > } > =20 > static int smbios_write_type1(ulong *current, int handle) > { > - struct smbios_type1 *t =3D (struct smbios_type1 *)*current; > + struct smbios_type1 *t; > int len =3D sizeof(struct smbios_type1); > char *serial_str =3D env_get("serial#"); > =20 > + t =3D map_sysmem(*current, len); > memset(t, 0, sizeof(struct smbios_type1)); > fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle); > t->manufacturer =3D smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTUR= ER); > @@ -122,15 +126,17 @@ static int smbios_write_type1(ulong *current, int h= andle) > =20 > len =3D t->length + smbios_string_table_len(t->eos); > *current +=3D len; > + unmap_sysmem(t); > =20 > return len; > } > =20 > static int smbios_write_type2(ulong *current, int handle) > { > - struct smbios_type2 *t =3D (struct smbios_type2 *)*current; > + struct smbios_type2 *t; > int len =3D sizeof(struct smbios_type2); > =20 > + t =3D map_sysmem(*current, len); > memset(t, 0, sizeof(struct smbios_type2)); > fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle); > t->manufacturer =3D smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTUR= ER); > @@ -140,15 +146,17 @@ static int smbios_write_type2(ulong *current, int h= andle) > =20 > len =3D t->length + smbios_string_table_len(t->eos); > *current +=3D len; > + unmap_sysmem(t); > =20 > return len; > } > =20 > static int smbios_write_type3(ulong *current, int handle) > { > - struct smbios_type3 *t =3D (struct smbios_type3 *)*current; > + struct smbios_type3 *t; > int len =3D sizeof(struct smbios_type3); > =20 > + t =3D map_sysmem(*current, len); > memset(t, 0, sizeof(struct smbios_type3)); > fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle); > t->manufacturer =3D smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTUR= ER); > @@ -160,6 +168,7 @@ static int smbios_write_type3(ulong *current, int han= dle) > =20 > len =3D t->length + smbios_string_table_len(t->eos); > *current +=3D len; > + unmap_sysmem(t); > =20 > return len; > } > @@ -198,9 +207,10 @@ static void smbios_write_type4_dm(struct smbios_type= 4 *t) > =20 > static int smbios_write_type4(ulong *current, int handle) > { > - struct smbios_type4 *t =3D (struct smbios_type4 *)*current; > + struct smbios_type4 *t; > int len =3D sizeof(struct smbios_type4); > =20 > + t =3D map_sysmem(*current, len); > memset(t, 0, sizeof(struct smbios_type4)); > fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle); > t->processor_type =3D SMBIOS_PROCESSOR_TYPE_CENTRAL; > @@ -214,32 +224,37 @@ static int smbios_write_type4(ulong *current, int h= andle) > =20 > len =3D t->length + smbios_string_table_len(t->eos); > *current +=3D len; > + unmap_sysmem(t); > =20 > return len; > } > =20 > static int smbios_write_type32(ulong *current, int handle) > { > - struct smbios_type32 *t =3D (struct smbios_type32 *)*current; > + struct smbios_type32 *t; > int len =3D sizeof(struct smbios_type32); > =20 > + t =3D map_sysmem(*current, len); > memset(t, 0, sizeof(struct smbios_type32)); > fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle); > =20 > *current +=3D len; > + unmap_sysmem(t); > =20 > return len; > } > =20 > static int smbios_write_type127(ulong *current, int handle) > { > - struct smbios_type127 *t =3D (struct smbios_type127 *)*current; > + struct smbios_type127 *t; > int len =3D sizeof(struct smbios_type127); > =20 > + t =3D map_sysmem(*current, len); > memset(t, 0, sizeof(struct smbios_type127)); > fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle); > =20 > *current +=3D len; > + unmap_sysmem(t); > =20 > return len; > } > @@ -257,6 +272,7 @@ static smbios_write_type smbios_write_funcs[] =3D { > ulong write_smbios_table(ulong addr) > { > struct smbios_entry *se; > + ulong table_addr; > ulong tables; > int len =3D 0; > int max_struct_size =3D 0; > @@ -268,7 +284,7 @@ ulong write_smbios_table(ulong addr) > /* 16 byte align the table address */ > addr =3D ALIGN(addr, 16); > =20 > - se =3D (struct smbios_entry *)(uintptr_t)addr; > + se =3D map_sysmem(addr, sizeof(struct smbios_entry)); > memset(se, 0, sizeof(struct smbios_entry)); > =20 > addr +=3D sizeof(struct smbios_entry); > @@ -290,7 +306,24 @@ ulong write_smbios_table(ulong addr) > se->max_struct_size =3D max_struct_size; > memcpy(se->intermediate_anchor, "_DMI_", 5); > se->struct_table_length =3D len; > - se->struct_table_address =3D tables; > + > + /* > + * We must use a pointer here so things work correctly on sandbox. The > + * user of this table is not aware of the mapping of addresses to > + * sandbox's DRAM buffer. > + */ > + table_addr =3D (ulong)map_sysmem(tables, 0); > + if (sizeof(table_addr) >=3D sizeof(u32) && table_addr >=3D (1ULL << 32)= ) { sizeof(long) >=3D sizeof(u32) will always be true on any platform U-Boot=20 supports, no? How about =C2=A0 if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MA= X) > + /* > + * We need to put this >32-bit pointer into the table but the > + * field is only 32 bits wide. > + */ > + printf("WARNING: SMBIOS table_address overflow %llx\n", > + (unsigned long long)table_addr); > + table_addr =3D 0; I'm also not sure what to do about the error case. IMHO ideally we=20 should propagate the error to the upper layers, but consider it non-fatal. > + } > + se->struct_table_address =3D table_addr; > + > se->struct_count =3D handle; > =20 > /* calculate checksums */ > @@ -298,6 +331,7 @@ ulong write_smbios_table(ulong addr) > isize =3D sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET; > se->intermediate_checksum =3D table_compute_checksum(istart, isize); > se->checksum =3D table_compute_checksum(se, sizeof(struct smbios_entry= )); > + unmap_sysmem(se); > =20 > return addr; > }