1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) 2 // 3 // This file is provided under a dual BSD/GPLv2 license. When using or 4 // redistributing this file, you may do so under either license. 5 // 6 // Copyright(c) 2022 Intel Corporation. All rights reserved. 7 8 #include <linux/firmware.h> 9 #include "sof-priv.h" 10 #include "sof-audio.h" 11 #include "ipc3-priv.h" 12 #include "ops.h" 13 14 static int ipc3_fw_ext_man_get_version(struct snd_sof_dev *sdev, 15 const struct sof_ext_man_elem_header *hdr) 16 { 17 const struct sof_ext_man_fw_version *v = 18 container_of(hdr, struct sof_ext_man_fw_version, hdr); 19 20 memcpy(&sdev->fw_ready.version, &v->version, sizeof(v->version)); 21 sdev->fw_ready.flags = v->flags; 22 23 /* log ABI versions and check FW compatibility */ 24 return sof_ipc3_validate_fw_version(sdev); 25 } 26 27 static int ipc3_fw_ext_man_get_windows(struct snd_sof_dev *sdev, 28 const struct sof_ext_man_elem_header *hdr) 29 { 30 const struct sof_ext_man_window *w; 31 32 w = container_of(hdr, struct sof_ext_man_window, hdr); 33 34 return sof_ipc3_get_ext_windows(sdev, &w->ipc_window.ext_hdr); 35 } 36 37 static int ipc3_fw_ext_man_get_cc_info(struct snd_sof_dev *sdev, 38 const struct sof_ext_man_elem_header *hdr) 39 { 40 const struct sof_ext_man_cc_version *cc; 41 42 cc = container_of(hdr, struct sof_ext_man_cc_version, hdr); 43 44 return sof_ipc3_get_cc_info(sdev, &cc->cc_version.ext_hdr); 45 } 46 47 static int ipc3_fw_ext_man_get_dbg_abi_info(struct snd_sof_dev *sdev, 48 const struct sof_ext_man_elem_header *hdr) 49 { 50 const struct ext_man_dbg_abi *dbg_abi = 51 container_of(hdr, struct ext_man_dbg_abi, hdr); 52 53 if (sdev->first_boot) 54 dev_dbg(sdev->dev, 55 "Firmware: DBG_ABI %d:%d:%d\n", 56 SOF_ABI_VERSION_MAJOR(dbg_abi->dbg_abi.abi_dbg_version), 57 SOF_ABI_VERSION_MINOR(dbg_abi->dbg_abi.abi_dbg_version), 58 SOF_ABI_VERSION_PATCH(dbg_abi->dbg_abi.abi_dbg_version)); 59 60 return 0; 61 } 62 63 static int ipc3_fw_ext_man_get_config_data(struct snd_sof_dev *sdev, 64 const struct sof_ext_man_elem_header *hdr) 65 { 66 const struct sof_ext_man_config_data *config = 67 container_of(hdr, struct sof_ext_man_config_data, hdr); 68 const struct sof_config_elem *elem; 69 int elems_counter; 70 int elems_size; 71 int ret = 0; 72 int i; 73 74 /* calculate elements counter */ 75 elems_size = config->hdr.size - sizeof(struct sof_ext_man_elem_header); 76 elems_counter = elems_size / sizeof(struct sof_config_elem); 77 78 dev_dbg(sdev->dev, "manifest can hold up to %d config elements\n", elems_counter); 79 80 for (i = 0; i < elems_counter; ++i) { 81 elem = &config->elems[i]; 82 dev_dbg(sdev->dev, "get index %d token %d val %d\n", 83 i, elem->token, elem->value); 84 switch (elem->token) { 85 case SOF_EXT_MAN_CONFIG_EMPTY: 86 /* unused memory space is zero filled - mapped to EMPTY elements */ 87 break; 88 case SOF_EXT_MAN_CONFIG_IPC_MSG_SIZE: 89 /* TODO: use ipc msg size from config data */ 90 break; 91 case SOF_EXT_MAN_CONFIG_MEMORY_USAGE_SCAN: 92 if (sdev->first_boot && elem->value) 93 ret = snd_sof_dbg_memory_info_init(sdev); 94 break; 95 default: 96 dev_info(sdev->dev, 97 "Unknown firmware configuration token %d value %d", 98 elem->token, elem->value); 99 break; 100 } 101 if (ret < 0) { 102 dev_err(sdev->dev, 103 "%s: processing failed for token %d value %#x, %d\n", 104 __func__, elem->token, elem->value, ret); 105 return ret; 106 } 107 } 108 109 return 0; 110 } 111 112 static ssize_t ipc3_fw_ext_man_size(const struct firmware *fw) 113 { 114 const struct sof_ext_man_header *head; 115 116 head = (struct sof_ext_man_header *)fw->data; 117 118 /* 119 * assert fw size is big enough to contain extended manifest header, 120 * it prevents from reading unallocated memory from `head` in following 121 * step. 122 */ 123 if (fw->size < sizeof(*head)) 124 return -EINVAL; 125 126 /* 127 * When fw points to extended manifest, 128 * then first u32 must be equal SOF_EXT_MAN_MAGIC_NUMBER. 129 */ 130 if (head->magic == SOF_EXT_MAN_MAGIC_NUMBER) 131 return head->full_size; 132 133 /* otherwise given fw don't have an extended manifest */ 134 return 0; 135 } 136 137 static size_t sof_ipc3_fw_parse_ext_man(struct snd_sof_dev *sdev) 138 { 139 struct snd_sof_pdata *plat_data = sdev->pdata; 140 const struct firmware *fw = plat_data->fw; 141 const struct sof_ext_man_elem_header *elem_hdr; 142 const struct sof_ext_man_header *head; 143 ssize_t ext_man_size; 144 ssize_t remaining; 145 uintptr_t iptr; 146 int ret = 0; 147 148 head = (struct sof_ext_man_header *)fw->data; 149 remaining = head->full_size - head->header_size; 150 ext_man_size = ipc3_fw_ext_man_size(fw); 151 152 /* Assert firmware starts with extended manifest */ 153 if (ext_man_size <= 0) 154 return ext_man_size; 155 156 /* incompatible version */ 157 if (SOF_EXT_MAN_VERSION_INCOMPATIBLE(SOF_EXT_MAN_VERSION, 158 head->header_version)) { 159 dev_err(sdev->dev, 160 "extended manifest version %#x differ from used %#x\n", 161 head->header_version, SOF_EXT_MAN_VERSION); 162 return -EINVAL; 163 } 164 165 /* get first extended manifest element header */ 166 iptr = (uintptr_t)fw->data + head->header_size; 167 168 while (remaining > sizeof(*elem_hdr)) { 169 elem_hdr = (struct sof_ext_man_elem_header *)iptr; 170 171 dev_dbg(sdev->dev, "found sof_ext_man header type %d size %#x\n", 172 elem_hdr->type, elem_hdr->size); 173 174 if (elem_hdr->size < sizeof(*elem_hdr) || 175 elem_hdr->size > remaining) { 176 dev_err(sdev->dev, 177 "invalid sof_ext_man header size, type %d size %#x\n", 178 elem_hdr->type, elem_hdr->size); 179 return -EINVAL; 180 } 181 182 /* process structure data */ 183 switch (elem_hdr->type) { 184 case SOF_EXT_MAN_ELEM_FW_VERSION: 185 ret = ipc3_fw_ext_man_get_version(sdev, elem_hdr); 186 break; 187 case SOF_EXT_MAN_ELEM_WINDOW: 188 ret = ipc3_fw_ext_man_get_windows(sdev, elem_hdr); 189 break; 190 case SOF_EXT_MAN_ELEM_CC_VERSION: 191 ret = ipc3_fw_ext_man_get_cc_info(sdev, elem_hdr); 192 break; 193 case SOF_EXT_MAN_ELEM_DBG_ABI: 194 ret = ipc3_fw_ext_man_get_dbg_abi_info(sdev, elem_hdr); 195 break; 196 case SOF_EXT_MAN_ELEM_CONFIG_DATA: 197 ret = ipc3_fw_ext_man_get_config_data(sdev, elem_hdr); 198 break; 199 case SOF_EXT_MAN_ELEM_PLATFORM_CONFIG_DATA: 200 ret = snd_sof_dsp_parse_platform_ext_manifest(sdev, elem_hdr); 201 break; 202 default: 203 dev_info(sdev->dev, 204 "unknown sof_ext_man header type %d size %#x\n", 205 elem_hdr->type, elem_hdr->size); 206 break; 207 } 208 209 if (ret < 0) { 210 dev_err(sdev->dev, 211 "failed to parse sof_ext_man header type %d size %#x\n", 212 elem_hdr->type, elem_hdr->size); 213 return ret; 214 } 215 216 remaining -= elem_hdr->size; 217 iptr += elem_hdr->size; 218 } 219 220 if (remaining) { 221 dev_err(sdev->dev, "error: sof_ext_man header is inconsistent\n"); 222 return -EINVAL; 223 } 224 225 return ext_man_size; 226 } 227 228 /* generic module parser for mmaped DSPs */ 229 static int sof_ipc3_parse_module_memcpy(struct snd_sof_dev *sdev, 230 struct snd_sof_mod_hdr *module) 231 { 232 struct snd_sof_blk_hdr *block; 233 int count, ret; 234 u32 offset; 235 size_t remaining; 236 237 dev_dbg(sdev->dev, "new module size %#x blocks %#x type %#x\n", 238 module->size, module->num_blocks, module->type); 239 240 block = (struct snd_sof_blk_hdr *)((u8 *)module + sizeof(*module)); 241 242 /* module->size doesn't include header size */ 243 remaining = module->size; 244 for (count = 0; count < module->num_blocks; count++) { 245 /* check for wrap */ 246 if (remaining < sizeof(*block)) { 247 dev_err(sdev->dev, "not enough data remaining\n"); 248 return -EINVAL; 249 } 250 251 /* minus header size of block */ 252 remaining -= sizeof(*block); 253 254 if (block->size == 0) { 255 dev_warn(sdev->dev, 256 "warning: block %d size zero\n", count); 257 dev_warn(sdev->dev, " type %#x offset %#x\n", 258 block->type, block->offset); 259 continue; 260 } 261 262 switch (block->type) { 263 case SOF_FW_BLK_TYPE_RSRVD0: 264 case SOF_FW_BLK_TYPE_ROM...SOF_FW_BLK_TYPE_RSRVD14: 265 continue; /* not handled atm */ 266 case SOF_FW_BLK_TYPE_IRAM: 267 case SOF_FW_BLK_TYPE_DRAM: 268 case SOF_FW_BLK_TYPE_SRAM: 269 offset = block->offset; 270 break; 271 default: 272 dev_err(sdev->dev, "%s: bad type %#x for block %#x\n", 273 __func__, block->type, count); 274 return -EINVAL; 275 } 276 277 dev_dbg(sdev->dev, "block %d type %#x size %#x ==> offset %#x\n", 278 count, block->type, block->size, offset); 279 280 /* checking block->size to avoid unaligned access */ 281 if (block->size % sizeof(u32)) { 282 dev_err(sdev->dev, "%s: invalid block size %#x\n", 283 __func__, block->size); 284 return -EINVAL; 285 } 286 ret = snd_sof_dsp_block_write(sdev, block->type, offset, 287 block + 1, block->size); 288 if (ret < 0) { 289 dev_err(sdev->dev, "%s: write to block type %#x failed\n", 290 __func__, block->type); 291 return ret; 292 } 293 294 if (remaining < block->size) { 295 dev_err(sdev->dev, "%s: not enough data remaining\n", __func__); 296 return -EINVAL; 297 } 298 299 /* minus body size of block */ 300 remaining -= block->size; 301 /* next block */ 302 block = (struct snd_sof_blk_hdr *)((u8 *)block + sizeof(*block) 303 + block->size); 304 } 305 306 return 0; 307 } 308 309 static int sof_ipc3_load_fw_to_dsp(struct snd_sof_dev *sdev) 310 { 311 struct snd_sof_pdata *plat_data = sdev->pdata; 312 const struct firmware *fw = plat_data->fw; 313 struct snd_sof_fw_header *header; 314 struct snd_sof_mod_hdr *module; 315 int (*load_module)(struct snd_sof_dev *sof_dev, struct snd_sof_mod_hdr *hdr); 316 size_t remaining; 317 int ret, count; 318 319 if (!plat_data->fw) 320 return -EINVAL; 321 322 header = (struct snd_sof_fw_header *)(fw->data + plat_data->fw_offset); 323 load_module = sof_ops(sdev)->load_module; 324 if (!load_module) { 325 dev_dbg(sdev->dev, "Using generic module loading\n"); 326 load_module = sof_ipc3_parse_module_memcpy; 327 } else { 328 dev_dbg(sdev->dev, "Using custom module loading\n"); 329 } 330 331 /* parse each module */ 332 module = (struct snd_sof_mod_hdr *)(fw->data + plat_data->fw_offset + 333 sizeof(*header)); 334 remaining = fw->size - sizeof(*header) - plat_data->fw_offset; 335 /* check for wrap */ 336 if (remaining > fw->size) { 337 dev_err(sdev->dev, "%s: fw size smaller than header size\n", __func__); 338 return -EINVAL; 339 } 340 341 for (count = 0; count < header->num_modules; count++) { 342 /* check for wrap */ 343 if (remaining < sizeof(*module)) { 344 dev_err(sdev->dev, "%s: not enough data for a module\n", 345 __func__); 346 return -EINVAL; 347 } 348 349 /* minus header size of module */ 350 remaining -= sizeof(*module); 351 352 /* module */ 353 ret = load_module(sdev, module); 354 if (ret < 0) { 355 dev_err(sdev->dev, "%s: invalid module %d\n", __func__, count); 356 return ret; 357 } 358 359 if (remaining < module->size) { 360 dev_err(sdev->dev, "%s: not enough data remaining\n", __func__); 361 return -EINVAL; 362 } 363 364 /* minus body size of module */ 365 remaining -= module->size; 366 module = (struct snd_sof_mod_hdr *)((u8 *)module + 367 sizeof(*module) + module->size); 368 } 369 370 return 0; 371 } 372 373 static int sof_ipc3_validate_firmware(struct snd_sof_dev *sdev) 374 { 375 struct snd_sof_pdata *plat_data = sdev->pdata; 376 const struct firmware *fw = plat_data->fw; 377 struct snd_sof_fw_header *header; 378 size_t fw_size = fw->size - plat_data->fw_offset; 379 380 if (fw->size <= plat_data->fw_offset) { 381 dev_err(sdev->dev, 382 "firmware size must be greater than firmware offset\n"); 383 return -EINVAL; 384 } 385 386 /* Read the header information from the data pointer */ 387 header = (struct snd_sof_fw_header *)(fw->data + plat_data->fw_offset); 388 389 /* verify FW sig */ 390 if (strncmp(header->sig, SND_SOF_FW_SIG, SND_SOF_FW_SIG_SIZE) != 0) { 391 dev_err(sdev->dev, "invalid firmware signature\n"); 392 return -EINVAL; 393 } 394 395 /* check size is valid */ 396 if (fw_size != header->file_size + sizeof(*header)) { 397 dev_err(sdev->dev, 398 "invalid filesize mismatch got 0x%zx expected 0x%zx\n", 399 fw_size, header->file_size + sizeof(*header)); 400 return -EINVAL; 401 } 402 403 dev_dbg(sdev->dev, "header size=0x%x modules=0x%x abi=0x%x size=%zu\n", 404 header->file_size, header->num_modules, 405 header->abi, sizeof(*header)); 406 407 return 0; 408 } 409 410 const struct sof_ipc_fw_loader_ops ipc3_loader_ops = { 411 .validate = sof_ipc3_validate_firmware, 412 .parse_ext_manifest = sof_ipc3_fw_parse_ext_man, 413 .load_fw_to_dsp = sof_ipc3_load_fw_to_dsp, 414 }; 415