1 // SPDX-License-Identifier: (GPL-2.0-only OR B 1 2 // 3 // This file is provided under a dual BSD/GPLv 4 // redistributing this file, you may do so und 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 15 const s 16 { 17 const struct sof_ext_man_fw_version *v 18 container_of(hdr, struct sof_e 19 20 memcpy(&sdev->fw_ready.version, &v->ve 21 sdev->fw_ready.flags = v->flags; 22 23 /* log ABI versions and check FW compa 24 return sof_ipc3_validate_fw_version(sd 25 } 26 27 static int ipc3_fw_ext_man_get_windows(struct 28 const s 29 { 30 const struct sof_ext_man_window *w; 31 32 w = container_of(hdr, struct sof_ext_m 33 34 return sof_ipc3_get_ext_windows(sdev, 35 } 36 37 static int ipc3_fw_ext_man_get_cc_info(struct 38 const s 39 { 40 const struct sof_ext_man_cc_version *c 41 42 cc = container_of(hdr, struct sof_ext_ 43 44 return sof_ipc3_get_cc_info(sdev, &cc- 45 } 46 47 static int ipc3_fw_ext_man_get_dbg_abi_info(st 48 co 49 { 50 const struct ext_man_dbg_abi *dbg_abi 51 container_of(hdr, struct ext_m 52 53 if (sdev->first_boot) 54 dev_dbg(sdev->dev, 55 "Firmware: DBG_ABI %d: 56 SOF_ABI_VERSION_MAJOR( 57 SOF_ABI_VERSION_MINOR( 58 SOF_ABI_VERSION_PATCH( 59 60 return 0; 61 } 62 63 static int ipc3_fw_ext_man_get_config_data(str 64 con 65 { 66 const struct sof_ext_man_config_data * 67 container_of(hdr, struct sof_e 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 76 elems_counter = elems_size / sizeof(st 77 78 dev_dbg(sdev->dev, "manifest can hold 79 80 for (i = 0; i < elems_counter; ++i) { 81 elem = &config->elems[i]; 82 dev_dbg(sdev->dev, "get index 83 i, elem->token, elem-> 84 switch (elem->token) { 85 case SOF_EXT_MAN_CONFIG_EMPTY: 86 /* unused memory space 87 break; 88 case SOF_EXT_MAN_CONFIG_IPC_MS 89 /* TODO: use ipc msg s 90 break; 91 case SOF_EXT_MAN_CONFIG_MEMORY 92 if (sdev->first_boot & 93 ret = snd_sof_ 94 break; 95 default: 96 dev_info(sdev->dev, 97 "Unknown firm 98 elem->token, 99 break; 100 } 101 if (ret < 0) { 102 dev_err(sdev->dev, 103 "%s: processin 104 __func__, elem 105 return ret; 106 } 107 } 108 109 return 0; 110 } 111 112 static ssize_t ipc3_fw_ext_man_size(struct snd 113 { 114 const struct sof_ext_man_header *head; 115 116 head = (struct sof_ext_man_header *)fw 117 118 /* 119 * assert fw size is big enough to con 120 * it prevents from reading unallocate 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_EX 129 */ 130 if (head->magic == SOF_EXT_MAN_MAGIC_N 131 return head->full_size; 132 133 /* otherwise given fw don't have an ex 134 dev_dbg(sdev->dev, "Unexpected extende 135 head->magic); 136 return 0; 137 } 138 139 static size_t sof_ipc3_fw_parse_ext_man(struct 140 { 141 const struct firmware *fw = sdev->base 142 const struct sof_ext_man_elem_header * 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 150 remaining = head->full_size - head->he 151 if (remaining < 0 || remaining > sdev- 152 return -EINVAL; 153 ext_man_size = ipc3_fw_ext_man_size(sd 154 155 /* Assert firmware starts with extende 156 if (ext_man_size <= 0) 157 return ext_man_size; 158 159 /* incompatible version */ 160 if (SOF_EXT_MAN_VERSION_INCOMPATIBLE(S 161 h 162 dev_err(sdev->dev, 163 "extended manifest ver 164 head->header_version, 165 return -EINVAL; 166 } 167 168 /* get first extended manifest element 169 iptr = (uintptr_t)fw->data + head->hea 170 171 while (remaining > sizeof(*elem_hdr)) 172 elem_hdr = (struct sof_ext_man 173 174 dev_dbg(sdev->dev, "found sof_ 175 elem_hdr->type, elem_h 176 177 if (elem_hdr->size < sizeof(*e 178 elem_hdr->size > remaining 179 dev_err(sdev->dev, 180 "invalid sof_e 181 elem_hdr->type 182 return -EINVAL; 183 } 184 185 /* process structure data */ 186 switch (elem_hdr->type) { 187 case SOF_EXT_MAN_ELEM_FW_VERSI 188 ret = ipc3_fw_ext_man_ 189 break; 190 case SOF_EXT_MAN_ELEM_WINDOW: 191 ret = ipc3_fw_ext_man_ 192 break; 193 case SOF_EXT_MAN_ELEM_CC_VERSI 194 ret = ipc3_fw_ext_man_ 195 break; 196 case SOF_EXT_MAN_ELEM_DBG_ABI: 197 ret = ipc3_fw_ext_man_ 198 break; 199 case SOF_EXT_MAN_ELEM_CONFIG_D 200 ret = ipc3_fw_ext_man_ 201 break; 202 case SOF_EXT_MAN_ELEM_PLATFORM 203 ret = snd_sof_dsp_pars 204 break; 205 default: 206 dev_info(sdev->dev, 207 "unknown sof_ 208 elem_hdr->typ 209 break; 210 } 211 212 if (ret < 0) { 213 dev_err(sdev->dev, 214 "failed to par 215 elem_hdr->type 216 return ret; 217 } 218 219 remaining -= elem_hdr->size; 220 iptr += elem_hdr->size; 221 } 222 223 if (remaining) { 224 dev_err(sdev->dev, "error: sof 225 return -EINVAL; 226 } 227 228 return ext_man_size; 229 } 230 231 /* generic module parser for mmaped DSPs */ 232 static int sof_ipc3_parse_module_memcpy(struct 233 struct 234 { 235 struct snd_sof_blk_hdr *block; 236 int count, ret; 237 u32 offset; 238 size_t remaining; 239 240 dev_dbg(sdev->dev, "new module size %# 241 module->size, module->num_bloc 242 243 block = (struct snd_sof_blk_hdr *)((u8 244 245 /* module->size doesn't include header 246 remaining = module->size; 247 for (count = 0; count < module->num_bl 248 /* check for wrap */ 249 if (remaining < sizeof(*block) 250 dev_err(sdev->dev, "no 251 return -EINVAL; 252 } 253 254 /* minus header size of block 255 remaining -= sizeof(*block); 256 257 if (block->size == 0) { 258 dev_warn(sdev->dev, 259 "warning: blo 260 dev_warn(sdev->dev, " 261 block->type, 262 continue; 263 } 264 265 switch (block->type) { 266 case SOF_FW_BLK_TYPE_RSRVD0: 267 case SOF_FW_BLK_TYPE_ROM...SOF 268 continue; /* not 269 case SOF_FW_BLK_TYPE_IRAM: 270 case SOF_FW_BLK_TYPE_DRAM: 271 case SOF_FW_BLK_TYPE_SRAM: 272 offset = block->offset 273 break; 274 default: 275 dev_err(sdev->dev, "%s 276 __func__, bloc 277 return -EINVAL; 278 } 279 280 dev_dbg(sdev->dev, "block %d t 281 count, block->type, bl 282 283 /* checking block->size to avo 284 if (block->size % sizeof(u32)) 285 dev_err(sdev->dev, "%s 286 __func__, bloc 287 return -EINVAL; 288 } 289 ret = snd_sof_dsp_block_write( 290 291 if (ret < 0) { 292 dev_err(sdev->dev, "%s 293 __func__, bloc 294 return ret; 295 } 296 297 if (remaining < block->size) { 298 dev_err(sdev->dev, "%s 299 return -EINVAL; 300 } 301 302 /* minus body size of block */ 303 remaining -= block->size; 304 /* next block */ 305 block = (struct snd_sof_blk_hd 306 + block->size); 307 } 308 309 return 0; 310 } 311 312 static int sof_ipc3_load_fw_to_dsp(struct snd_ 313 { 314 u32 payload_offset = sdev->basefw.payl 315 const struct firmware *fw = sdev->base 316 struct snd_sof_fw_header *header; 317 struct snd_sof_mod_hdr *module; 318 int (*load_module)(struct snd_sof_dev 319 size_t remaining; 320 int ret, count; 321 322 if (!fw) 323 return -EINVAL; 324 325 header = (struct snd_sof_fw_header *)( 326 load_module = sof_ops(sdev)->load_modu 327 if (!load_module) { 328 dev_dbg(sdev->dev, "Using gene 329 load_module = sof_ipc3_parse_m 330 } else { 331 dev_dbg(sdev->dev, "Using cust 332 } 333 334 /* parse each module */ 335 module = (struct snd_sof_mod_hdr *)(fw 336 remaining = fw->size - sizeof(*header) 337 /* check for wrap */ 338 if (remaining > fw->size) { 339 dev_err(sdev->dev, "%s: fw siz 340 return -EINVAL; 341 } 342 343 for (count = 0; count < header->num_mo 344 /* check for wrap */ 345 if (remaining < sizeof(*module 346 dev_err(sdev->dev, "%s 347 __func__); 348 return -EINVAL; 349 } 350 351 /* minus header size of module 352 remaining -= sizeof(*module); 353 354 /* module */ 355 ret = load_module(sdev, module 356 if (ret < 0) { 357 dev_err(sdev->dev, "%s 358 return ret; 359 } 360 361 if (remaining < module->size) 362 dev_err(sdev->dev, "%s 363 return -EINVAL; 364 } 365 366 /* minus body size of module * 367 remaining -= module->size; 368 module = (struct snd_sof_mod_h 369 sizeof(*module) + mod 370 } 371 372 return 0; 373 } 374 375 static int sof_ipc3_validate_firmware(struct s 376 { 377 u32 payload_offset = sdev->basefw.payl 378 const struct firmware *fw = sdev->base 379 struct snd_sof_fw_header *header; 380 size_t fw_size = fw->size - payload_of 381 382 if (fw->size <= payload_offset) { 383 dev_err(sdev->dev, 384 "firmware size must be 385 return -EINVAL; 386 } 387 388 /* Read the header information from th 389 header = (struct snd_sof_fw_header *)( 390 391 /* verify FW sig */ 392 if (strncmp(header->sig, SND_SOF_FW_SI 393 dev_err(sdev->dev, "invalid fi 394 return -EINVAL; 395 } 396 397 /* check size is valid */ 398 if (fw_size != header->file_size + siz 399 dev_err(sdev->dev, 400 "invalid filesize mism 401 fw_size, header->file_ 402 return -EINVAL; 403 } 404 405 dev_dbg(sdev->dev, "header size=0x%x m 406 header->file_size, header->num 407 header->abi, sizeof(*header)); 408 409 return 0; 410 } 411 412 const struct sof_ipc_fw_loader_ops ipc3_loader 413 .validate = sof_ipc3_validate_firmware 414 .parse_ext_manifest = sof_ipc3_fw_pars 415 .load_fw_to_dsp = sof_ipc3_load_fw_to_ 416 }; 417
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.