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 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(struct snd_sof_dev *sdev, 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 dev_dbg(sdev->dev, "Unexpected extended manifest magic number: %#x\n", 135 head->magic); 136 return 0; 137 } 138 139 static size_t sof_ipc3_fw_parse_ext_man(struct snd_sof_dev *sdev) 140 { 141 const struct firmware *fw = sdev->basefw.fw; 142 const struct sof_ext_man_elem_header *elem_hdr; 143 const struct sof_ext_man_header *head; 144 ssize_t ext_man_size; 145 ssize_t remaining; 146 uintptr_t iptr; 147 int ret = 0; 148 149 head = (struct sof_ext_man_header *)fw->data; 150 remaining = head->full_size - head->header_size; 151 if (remaining < 0 || remaining > sdev->basefw.fw->size) 152 return -EINVAL; 153 ext_man_size = ipc3_fw_ext_man_size(sdev, fw); 154 155 /* Assert firmware starts with extended manifest */ 156 if (ext_man_size <= 0) 157 return ext_man_size; 158 159 /* incompatible version */ 160 if (SOF_EXT_MAN_VERSION_INCOMPATIBLE(SOF_EXT_MAN_VERSION, 161 head->header_version)) { 162 dev_err(sdev->dev, 163 "extended manifest version %#x differ from used %#x\n", 164 head->header_version, SOF_EXT_MAN_VERSION); 165 return -EINVAL; 166 } 167 168 /* get first extended manifest element header */ 169 iptr = (uintptr_t)fw->data + head->header_size; 170 171 while (remaining > sizeof(*elem_hdr)) { 172 elem_hdr = (struct sof_ext_man_elem_header *)iptr; 173 174 dev_dbg(sdev->dev, "found sof_ext_man header type %d size %#x\n", 175 elem_hdr->type, elem_hdr->size); 176 177 if (elem_hdr->size < sizeof(*elem_hdr) || 178 elem_hdr->size > remaining) { 179 dev_err(sdev->dev, 180 "invalid sof_ext_man header size, type %d size %#x\n", 181 elem_hdr->type, elem_hdr->size); 182 return -EINVAL; 183 } 184 185 /* process structure data */ 186 switch (elem_hdr->type) { 187 case SOF_EXT_MAN_ELEM_FW_VERSION: 188 ret = ipc3_fw_ext_man_get_version(sdev, elem_hdr); 189 break; 190 case SOF_EXT_MAN_ELEM_WINDOW: 191 ret = ipc3_fw_ext_man_get_windows(sdev, elem_hdr); 192 break; 193 case SOF_EXT_MAN_ELEM_CC_VERSION: 194 ret = ipc3_fw_ext_man_get_cc_info(sdev, elem_hdr); 195 break; 196 case SOF_EXT_MAN_ELEM_PROBE_INFO: 197 dev_dbg(sdev->dev, "Probe info (not parsed)\n"); 198 break; 199 case SOF_EXT_MAN_ELEM_DBG_ABI: 200 ret = ipc3_fw_ext_man_get_dbg_abi_info(sdev, elem_hdr); 201 break; 202 case SOF_EXT_MAN_ELEM_CONFIG_DATA: 203 ret = ipc3_fw_ext_man_get_config_data(sdev, elem_hdr); 204 break; 205 case SOF_EXT_MAN_ELEM_PLATFORM_CONFIG_DATA: 206 ret = snd_sof_dsp_parse_platform_ext_manifest(sdev, elem_hdr); 207 break; 208 default: 209 dev_info(sdev->dev, 210 "unknown sof_ext_man header type %d size %#x\n", 211 elem_hdr->type, elem_hdr->size); 212 break; 213 } 214 215 if (ret < 0) { 216 dev_err(sdev->dev, 217 "failed to parse sof_ext_man header type %d size %#x\n", 218 elem_hdr->type, elem_hdr->size); 219 return ret; 220 } 221 222 remaining -= elem_hdr->size; 223 iptr += elem_hdr->size; 224 } 225 226 if (remaining) { 227 dev_err(sdev->dev, "error: sof_ext_man header is inconsistent\n"); 228 return -EINVAL; 229 } 230 231 return ext_man_size; 232 } 233 234 /* generic module parser for mmaped DSPs */ 235 static int sof_ipc3_parse_module_memcpy(struct snd_sof_dev *sdev, 236 struct snd_sof_mod_hdr *module) 237 { 238 struct snd_sof_blk_hdr *block; 239 int count, ret; 240 u32 offset; 241 size_t remaining; 242 243 dev_dbg(sdev->dev, "new module size %#x blocks %#x type %#x\n", 244 module->size, module->num_blocks, module->type); 245 246 block = (struct snd_sof_blk_hdr *)((u8 *)module + sizeof(*module)); 247 248 /* module->size doesn't include header size */ 249 remaining = module->size; 250 for (count = 0; count < module->num_blocks; count++) { 251 /* check for wrap */ 252 if (remaining < sizeof(*block)) { 253 dev_err(sdev->dev, "not enough data remaining\n"); 254 return -EINVAL; 255 } 256 257 /* minus header size of block */ 258 remaining -= sizeof(*block); 259 260 if (block->size == 0) { 261 dev_warn(sdev->dev, 262 "warning: block %d size zero\n", count); 263 dev_warn(sdev->dev, " type %#x offset %#x\n", 264 block->type, block->offset); 265 continue; 266 } 267 268 switch (block->type) { 269 case SOF_FW_BLK_TYPE_RSRVD0: 270 case SOF_FW_BLK_TYPE_ROM...SOF_FW_BLK_TYPE_RSRVD14: 271 continue; /* not handled atm */ 272 case SOF_FW_BLK_TYPE_IRAM: 273 case SOF_FW_BLK_TYPE_DRAM: 274 case SOF_FW_BLK_TYPE_SRAM: 275 offset = block->offset; 276 break; 277 default: 278 dev_err(sdev->dev, "%s: bad type %#x for block %#x\n", 279 __func__, block->type, count); 280 return -EINVAL; 281 } 282 283 dev_dbg(sdev->dev, "block %d type %#x size %#x ==> offset %#x\n", 284 count, block->type, block->size, offset); 285 286 /* checking block->size to avoid unaligned access */ 287 if (block->size % sizeof(u32)) { 288 dev_err(sdev->dev, "%s: invalid block size %#x\n", 289 __func__, block->size); 290 return -EINVAL; 291 } 292 ret = snd_sof_dsp_block_write(sdev, block->type, offset, 293 block + 1, block->size); 294 if (ret < 0) { 295 dev_err(sdev->dev, "%s: write to block type %#x failed\n", 296 __func__, block->type); 297 return ret; 298 } 299 300 if (remaining < block->size) { 301 dev_err(sdev->dev, "%s: not enough data remaining\n", __func__); 302 return -EINVAL; 303 } 304 305 /* minus body size of block */ 306 remaining -= block->size; 307 /* next block */ 308 block = (struct snd_sof_blk_hdr *)((u8 *)block + sizeof(*block) 309 + block->size); 310 } 311 312 return 0; 313 } 314 315 static int sof_ipc3_load_fw_to_dsp(struct snd_sof_dev *sdev) 316 { 317 u32 payload_offset = sdev->basefw.payload_offset; 318 const struct firmware *fw = sdev->basefw.fw; 319 struct snd_sof_fw_header *header; 320 struct snd_sof_mod_hdr *module; 321 int (*load_module)(struct snd_sof_dev *sof_dev, struct snd_sof_mod_hdr *hdr); 322 size_t remaining; 323 int ret, count; 324 325 if (!fw) 326 return -EINVAL; 327 328 header = (struct snd_sof_fw_header *)(fw->data + payload_offset); 329 load_module = sof_ops(sdev)->load_module; 330 if (!load_module) { 331 dev_dbg(sdev->dev, "Using generic module loading\n"); 332 load_module = sof_ipc3_parse_module_memcpy; 333 } else { 334 dev_dbg(sdev->dev, "Using custom module loading\n"); 335 } 336 337 /* parse each module */ 338 module = (struct snd_sof_mod_hdr *)(fw->data + payload_offset + sizeof(*header)); 339 remaining = fw->size - sizeof(*header) - payload_offset; 340 /* check for wrap */ 341 if (remaining > fw->size) { 342 dev_err(sdev->dev, "%s: fw size smaller than header size\n", __func__); 343 return -EINVAL; 344 } 345 346 for (count = 0; count < header->num_modules; count++) { 347 /* check for wrap */ 348 if (remaining < sizeof(*module)) { 349 dev_err(sdev->dev, "%s: not enough data for a module\n", 350 __func__); 351 return -EINVAL; 352 } 353 354 /* minus header size of module */ 355 remaining -= sizeof(*module); 356 357 /* module */ 358 ret = load_module(sdev, module); 359 if (ret < 0) { 360 dev_err(sdev->dev, "%s: invalid module %d\n", __func__, count); 361 return ret; 362 } 363 364 if (remaining < module->size) { 365 dev_err(sdev->dev, "%s: not enough data remaining\n", __func__); 366 return -EINVAL; 367 } 368 369 /* minus body size of module */ 370 remaining -= module->size; 371 module = (struct snd_sof_mod_hdr *)((u8 *)module + 372 sizeof(*module) + module->size); 373 } 374 375 return 0; 376 } 377 378 static int sof_ipc3_validate_firmware(struct snd_sof_dev *sdev) 379 { 380 u32 payload_offset = sdev->basefw.payload_offset; 381 const struct firmware *fw = sdev->basefw.fw; 382 struct snd_sof_fw_header *header; 383 size_t fw_size = fw->size - payload_offset; 384 385 if (fw->size <= payload_offset) { 386 dev_err(sdev->dev, 387 "firmware size must be greater than firmware offset\n"); 388 return -EINVAL; 389 } 390 391 /* Read the header information from the data pointer */ 392 header = (struct snd_sof_fw_header *)(fw->data + payload_offset); 393 394 /* verify FW sig */ 395 if (strncmp(header->sig, SND_SOF_FW_SIG, SND_SOF_FW_SIG_SIZE) != 0) { 396 dev_err(sdev->dev, "invalid firmware signature\n"); 397 return -EINVAL; 398 } 399 400 /* check size is valid */ 401 if (fw_size != header->file_size + sizeof(*header)) { 402 dev_err(sdev->dev, 403 "invalid filesize mismatch got 0x%zx expected 0x%zx\n", 404 fw_size, header->file_size + sizeof(*header)); 405 return -EINVAL; 406 } 407 408 dev_dbg(sdev->dev, "header size=0x%x modules=0x%x abi=0x%x size=%zu\n", 409 header->file_size, header->num_modules, 410 header->abi, sizeof(*header)); 411 412 return 0; 413 } 414 415 const struct sof_ipc_fw_loader_ops ipc3_loader_ops = { 416 .validate = sof_ipc3_validate_firmware, 417 .parse_ext_manifest = sof_ipc3_fw_parse_ext_man, 418 .load_fw_to_dsp = sof_ipc3_load_fw_to_dsp, 419 }; 420