77#define pr_fmt (fmt ) KBUILD_MODNAME ": " fmt
88
99#include <linux/arm-smccc.h>
10+ #include <linux/cpuhotplug.h>
1011#include <linux/errno.h>
12+ #include <linux/firmware.h>
1113#include <linux/interrupt.h>
1214#include <linux/io.h>
1315#include <linux/irqdomain.h>
16+ #include <linux/kernel.h>
1417#include <linux/mm.h>
1518#include <linux/module.h>
1619#include <linux/of.h>
@@ -1263,6 +1266,22 @@ static bool optee_msg_api_uid_is_optee_api(optee_invoke_fn *invoke_fn)
12631266 return false;
12641267}
12651268
1269+ #ifdef CONFIG_OPTEE_INSECURE_LOAD_IMAGE
1270+ static bool optee_msg_api_uid_is_optee_image_load (optee_invoke_fn * invoke_fn )
1271+ {
1272+ struct arm_smccc_res res ;
1273+
1274+ invoke_fn (OPTEE_SMC_CALLS_UID , 0 , 0 , 0 , 0 , 0 , 0 , 0 , & res );
1275+
1276+ if (res .a0 == OPTEE_MSG_IMAGE_LOAD_UID_0 &&
1277+ res .a1 == OPTEE_MSG_IMAGE_LOAD_UID_1 &&
1278+ res .a2 == OPTEE_MSG_IMAGE_LOAD_UID_2 &&
1279+ res .a3 == OPTEE_MSG_IMAGE_LOAD_UID_3 )
1280+ return true;
1281+ return false;
1282+ }
1283+ #endif
1284+
12661285static void optee_msg_get_os_revision (optee_invoke_fn * invoke_fn )
12671286{
12681287 union {
@@ -1468,6 +1487,120 @@ static void optee_shutdown(struct platform_device *pdev)
14681487 optee_disable_shm_cache (optee );
14691488}
14701489
1490+ #ifdef CONFIG_OPTEE_INSECURE_LOAD_IMAGE
1491+
1492+ #define OPTEE_FW_IMAGE "optee/tee.bin"
1493+
1494+ static optee_invoke_fn * cpuhp_invoke_fn ;
1495+
1496+ static int optee_cpuhp_probe (unsigned int cpu )
1497+ {
1498+ /*
1499+ * Invoking a call on a CPU will cause OP-TEE to perform the required
1500+ * setup for that CPU. Just invoke the call to get the UID since that
1501+ * has no side effects.
1502+ */
1503+ if (optee_msg_api_uid_is_optee_api (cpuhp_invoke_fn ))
1504+ return 0 ;
1505+ else
1506+ return - EINVAL ;
1507+ }
1508+
1509+ static int optee_load_fw (struct platform_device * pdev ,
1510+ optee_invoke_fn * invoke_fn )
1511+ {
1512+ const struct firmware * fw = NULL ;
1513+ struct arm_smccc_res res ;
1514+ phys_addr_t data_pa ;
1515+ u8 * data_buf = NULL ;
1516+ u64 data_size ;
1517+ u32 data_pa_high , data_pa_low ;
1518+ u32 data_size_high , data_size_low ;
1519+ int rc ;
1520+ int hp_state ;
1521+
1522+ if (!optee_msg_api_uid_is_optee_image_load (invoke_fn ))
1523+ return 0 ;
1524+
1525+ rc = request_firmware (& fw , OPTEE_FW_IMAGE , & pdev -> dev );
1526+ if (rc ) {
1527+ /*
1528+ * The firmware in the rootfs will not be accessible until we
1529+ * are in the SYSTEM_RUNNING state, so return EPROBE_DEFER until
1530+ * that point.
1531+ */
1532+ if (system_state < SYSTEM_RUNNING )
1533+ return - EPROBE_DEFER ;
1534+ goto fw_err ;
1535+ }
1536+
1537+ data_size = fw -> size ;
1538+ /*
1539+ * This uses the GFP_DMA flag to ensure we are allocated memory in the
1540+ * 32-bit space since TF-A cannot map memory beyond the 32-bit boundary.
1541+ */
1542+ data_buf = kmalloc (fw -> size , GFP_KERNEL | GFP_DMA );
1543+ if (!data_buf ) {
1544+ rc = - ENOMEM ;
1545+ goto fw_err ;
1546+ }
1547+ memcpy (data_buf , fw -> data , fw -> size );
1548+ data_pa = virt_to_phys (data_buf );
1549+ reg_pair_from_64 (& data_pa_high , & data_pa_low , data_pa );
1550+ reg_pair_from_64 (& data_size_high , & data_size_low , data_size );
1551+ goto fw_load ;
1552+
1553+ fw_err :
1554+ pr_warn ("image loading failed\n" );
1555+ data_pa_high = 0 ;
1556+ data_pa_low = 0 ;
1557+ data_size_high = 0 ;
1558+ data_size_low = 0 ;
1559+
1560+ fw_load :
1561+ /*
1562+ * Always invoke the SMC, even if loading the image fails, to indicate
1563+ * to EL3 that we have passed the point where it should allow invoking
1564+ * this SMC.
1565+ */
1566+ pr_warn ("OP-TEE image loaded from kernel, this can be insecure" );
1567+ invoke_fn (OPTEE_SMC_CALL_LOAD_IMAGE , data_size_high , data_size_low ,
1568+ data_pa_high , data_pa_low , 0 , 0 , 0 , & res );
1569+ if (!rc )
1570+ rc = res .a0 ;
1571+ if (fw )
1572+ release_firmware (fw );
1573+ kfree (data_buf );
1574+
1575+ if (!rc ) {
1576+ /*
1577+ * We need to initialize OP-TEE on all other running cores as
1578+ * well. Any cores that aren't running yet will get initialized
1579+ * when they are brought up by the power management functions in
1580+ * TF-A which are registered by the OP-TEE SPD. Due to that we
1581+ * can un-register the callback right after registering it.
1582+ */
1583+ cpuhp_invoke_fn = invoke_fn ;
1584+ hp_state = cpuhp_setup_state (CPUHP_AP_ONLINE_DYN , "optee:probe" ,
1585+ optee_cpuhp_probe , NULL );
1586+ if (hp_state < 0 ) {
1587+ pr_warn ("Failed with CPU hotplug setup for OP-TEE" );
1588+ return - EINVAL ;
1589+ }
1590+ cpuhp_remove_state (hp_state );
1591+ cpuhp_invoke_fn = NULL ;
1592+ }
1593+
1594+ return rc ;
1595+ }
1596+ #else
1597+ static inline int optee_load_fw (struct platform_device * pdev ,
1598+ optee_invoke_fn * invoke_fn )
1599+ {
1600+ return 0 ;
1601+ }
1602+ #endif
1603+
14711604static int optee_probe (struct platform_device * pdev )
14721605{
14731606 optee_invoke_fn * invoke_fn ;
@@ -1486,6 +1619,10 @@ static int optee_probe(struct platform_device *pdev)
14861619 if (IS_ERR (invoke_fn ))
14871620 return PTR_ERR (invoke_fn );
14881621
1622+ rc = optee_load_fw (pdev , invoke_fn );
1623+ if (rc )
1624+ return rc ;
1625+
14891626 if (!optee_msg_api_uid_is_optee_api (invoke_fn )) {
14901627 pr_warn ("api uid mismatch\n" );
14911628 return - EINVAL ;
0 commit comments