From 4071b35235d0a181e620098f5ddaa3ea2d374969 Mon Sep 17 00:00:00 2001 From: Philip Elcan Date: Mon, 20 May 2019 16:57:17 -0400 Subject: [PATCH] gpu: nvgpu: bios: use memcpy to fix MISRA 11.3 bugs MISRA Rule 11.3 prohibits casting between different pointer types. The previous "fix" in nvgpu_bios_parse_rom() was to use an intermediate cast to uintptr_t. However, that leaves the possibility of creating a mis-aligned pointer. So, instead of casts, use nvgpu_memcpy() to make a copy of the data in a local structure. JIRA NVGPU-3317 Change-Id: I3f9dd0d6c10a7425f300b51410be2e248177b505 Signed-off-by: Philip Elcan Reviewed-on: https://git-master.nvidia.com/r/2122390 Reviewed-by: Thomas Fleury Reviewed-by: Alex Waterman Reviewed-by: mobile promotions Tested-by: mobile promotions --- drivers/gpu/nvgpu/common/vbios/bios.c | 72 +++++++++++++-------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/drivers/gpu/nvgpu/common/vbios/bios.c b/drivers/gpu/nvgpu/common/vbios/bios.c index ec2f1dfd0..1896a0a16 100644 --- a/drivers/gpu/nvgpu/common/vbios/bios.c +++ b/drivers/gpu/nvgpu/common/vbios/bios.c @@ -51,36 +51,35 @@ int nvgpu_bios_parse_rom(struct gk20a *g) unsigned int i; while (last == 0U) { - struct pci_exp_rom *pci_rom; - struct pci_data_struct *pci_data; - struct pci_ext_data_struct *pci_ext_data; + struct pci_exp_rom pci_rom; + struct pci_data_struct pci_data; + struct pci_ext_data_struct pci_ext_data; - pci_rom = (struct pci_exp_rom *)((uintptr_t)g->bios.data + - offset); + nvgpu_memcpy((u8 *)&pci_rom, (u8 *)(g->bios.data + offset), + sizeof(struct pci_exp_rom)); nvgpu_log_fn(g, "pci rom sig %04x ptr %04x block %x", - pci_rom->sig, pci_rom->pci_data_struct_ptr, - pci_rom->size_of_block); + pci_rom.sig, pci_rom.pci_data_struct_ptr, + pci_rom.size_of_block); - if (pci_rom->sig != PCI_EXP_ROM_SIG && - pci_rom->sig != PCI_EXP_ROM_SIG_NV) { + if (pci_rom.sig != PCI_EXP_ROM_SIG && + pci_rom.sig != PCI_EXP_ROM_SIG_NV) { nvgpu_err(g, "invalid VBIOS signature"); return -EINVAL; } - pci_data = - (struct pci_data_struct *) - ((uintptr_t)g->bios.data + offset + - pci_rom->pci_data_struct_ptr); + nvgpu_memcpy((u8 *)&pci_data, (u8 *)(g->bios.data + offset + + pci_rom.pci_data_struct_ptr), + sizeof(struct pci_data_struct)); nvgpu_log_fn(g, "pci data sig %08x len %d image len %x type %x last %d max %08x", - pci_data->sig, pci_data->pci_data_struct_len, - pci_data->image_len, pci_data->code_type, - pci_data->last_image, - pci_data->max_runtime_image_len); + pci_data.sig, pci_data.pci_data_struct_len, + pci_data.image_len, pci_data.code_type, + pci_data.last_image, + pci_data.max_runtime_image_len); /* Get Base ROM Size */ - if (pci_data->code_type == + if (pci_data.code_type == PCI_DATA_STRUCTURE_CODE_TYPE_VBIOS_BASE) { - g->bios.base_rom_size = (u32)pci_data->image_len * + g->bios.base_rom_size = (u32)pci_data.image_len * PCI_ROM_IMAGE_BLOCK_SIZE; nvgpu_log_fn(g, "Base ROM Size: %x", g->bios.base_rom_size); @@ -95,35 +94,36 @@ int nvgpu_bios_parse_rom(struct gk20a *g) * need to add the UEFI ROM size to offsets within the * expansion ROM. */ - if (pci_data->code_type == + if (pci_data.code_type == PCI_DATA_STRUCTURE_CODE_TYPE_VBIOS_UEFI) { u32 ext_offset = (offset + - pci_rom->pci_data_struct_ptr + - pci_data->pci_data_struct_len + + pci_rom.pci_data_struct_ptr + + pci_data.pci_data_struct_len + 0xfU) & ~0xfU; - pci_ext_data = (struct pci_ext_data_struct *) - ((uintptr_t)g->bios.data + ext_offset); + nvgpu_memcpy((u8 *)&pci_ext_data, (u8 *)(g->bios.data + + ext_offset), + sizeof(struct pci_ext_data_struct)); nvgpu_log_fn(g, "pci ext data sig %08x rev %x len %x sub_image_len %x priv_last %d flags %x", - pci_ext_data->sig, - pci_ext_data->nv_pci_data_ext_rev, - pci_ext_data->nv_pci_data_ext_len, - pci_ext_data->sub_image_len, - pci_ext_data->priv_last_image, - pci_ext_data->flags); + pci_ext_data.sig, + pci_ext_data.nv_pci_data_ext_rev, + pci_ext_data.nv_pci_data_ext_len, + pci_ext_data.sub_image_len, + pci_ext_data.priv_last_image, + pci_ext_data.flags); nvgpu_log_fn(g, "expansion rom offset %x", - pci_data->image_len * + pci_data.image_len * PCI_ROM_IMAGE_BLOCK_SIZE); g->bios.expansion_rom_offset = - (u32)pci_data->image_len * + (u32)pci_data.image_len * PCI_ROM_IMAGE_BLOCK_SIZE; - offset += (u32)pci_ext_data->sub_image_len * + offset += (u32)pci_ext_data.sub_image_len * PCI_ROM_IMAGE_BLOCK_SIZE; - last = pci_ext_data->priv_last_image; + last = pci_ext_data.priv_last_image; } else { - offset += (u32)pci_data->image_len * + offset += (u32)pci_data.image_len * PCI_ROM_IMAGE_BLOCK_SIZE; - last = pci_data->last_image; + last = pci_data.last_image; } }