1 /*
2  * Copyright (c) 2023, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <common/debug.h>
8 
9 #include "ethosn_big_fw.h"
10 
11 /* Magic (FourCC) number to identify the big firmware binary */
12 #define ETHOSN_BIG_FW_MAGIC	('E' | ('N' << 8) | ('F' << 16) | ('W' << 24))
13 
14 /* Supported big firmware version */
15 #define ETHOSN_BIG_FW_VERSION_MAJOR	15
16 
17 #define ETHOSN_ARCH_VER_MAJOR_MASK	U(0xF000)
18 #define ETHOSN_ARCH_VER_MAJOR_SHIFT	U(0xC)
19 #define ETHOSN_ARCH_VER_MINOR_MASK	U(0xF00)
20 #define ETHOSN_ARCH_VER_MINOR_SHIFT	U(0x8)
21 #define ETHOSN_ARCH_VER_REV_MASK	U(0xFF)
22 
23 /* Convert Arm(R) Ethos(TM)-N NPU architecture version to big firmware format */
24 #define ETHOSN_BIG_FW_FORMAT_ARCH_VER(arch_ver)					 \
25 	(arch_ver & ETHOSN_ARCH_VER_MAJOR_MASK) << ETHOSN_ARCH_VER_MAJOR_SHIFT | \
26 	(arch_ver & ETHOSN_ARCH_VER_MINOR_MASK) << ETHOSN_ARCH_VER_MINOR_SHIFT | \
27 	(arch_ver & ETHOSN_ARCH_VER_REV_MASK)
28 
29 
ethosn_big_fw_verify_header(const struct ethosn_big_fw * big_fw,uint32_t npu_arch_ver)30 bool ethosn_big_fw_verify_header(const struct ethosn_big_fw *big_fw,
31 				 uint32_t npu_arch_ver)
32 {
33 	const uint32_t arch_ver = ETHOSN_BIG_FW_FORMAT_ARCH_VER(npu_arch_ver);
34 
35 	if (big_fw->fw_magic != ETHOSN_BIG_FW_MAGIC) {
36 		ERROR("ETHOSN: Unable to find firmware. Invalid magic value: 0x%02x\n",
37 		      big_fw->fw_magic);
38 
39 		return false;
40 	}
41 
42 	if (big_fw->fw_ver_major != ETHOSN_BIG_FW_VERSION_MAJOR) {
43 		ERROR("ETHOSN: Unsupported firmware version: %u.%u.%u. Expected Version %u.x.x.\n",
44 		      big_fw->fw_ver_major, big_fw->fw_ver_minor,
45 		      big_fw->fw_ver_patch, ETHOSN_BIG_FW_VERSION_MAJOR);
46 
47 		return false;
48 	}
49 
50 	if (big_fw->arch_min > arch_ver || arch_ver > big_fw->arch_max) {
51 		ERROR("ETHOSN: Firmware is not compatbile with architecture version: 0x%02x\n",
52 		      npu_arch_ver);
53 		return false;
54 	}
55 
56 	return true;
57 }
58