• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Decription: function declaration for load app operations for kernel CA.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  */
14 #include "teek_app_load.h"
15 #include <linux/fs.h>
16 #include <linux/uaccess.h>
17 #include <linux/vmalloc.h>
18 #include "session_manager.h"
19 #include "ko_adapt.h"
20 
teek_open_app_file(struct file * fp,char ** file_buf,uint32_t total_img_len)21 static int32_t teek_open_app_file(struct file *fp, char **file_buf, uint32_t total_img_len)
22 {
23 	loff_t pos = 0;
24 	uint32_t read_size;
25 	char *file_buffer = NULL;
26 
27 	if (total_img_len == 0 || total_img_len > MAX_IMAGE_LEN) {
28 		tloge("img len is invalied %u\n", total_img_len);
29 		return TEEC_ERROR_BAD_PARAMETERS;
30 	}
31 
32 	file_buffer = vmalloc(total_img_len);
33 	if (!file_buffer) {
34 		tloge("alloc TA file buffer(size=%u) failed\n", total_img_len);
35 		return TEEC_ERROR_GENERIC;
36 	}
37 
38 	read_size = (uint32_t)kernel_read(fp, file_buffer, total_img_len, &pos);
39 	if (read_size != total_img_len) {
40 		tloge("read ta file failed, read size/total size=%u/%u\n", read_size, total_img_len);
41 		vfree(file_buffer);
42 		return TEEC_ERROR_GENERIC;
43 	}
44 
45 	*file_buf = file_buffer;
46 
47 	return TEEC_SUCCESS;
48 }
49 
teek_read_app(const char * load_file,char ** file_buf,uint32_t * file_len)50 static int32_t teek_read_app(const char *load_file, char **file_buf, uint32_t *file_len)
51 {
52 	int32_t ret;
53 	struct file *fp = NULL;
54 
55 	fp = filp_open(load_file, O_RDONLY, 0);
56 	if (!fp || IS_ERR(fp)) {
57 		tloge("open file error %ld\n", PTR_ERR(fp));
58 		return TEEC_ERROR_BAD_PARAMETERS;
59 	}
60 
61 	if (!fp->f_inode) {
62 		tloge("node is NULL\n");
63 		filp_close(fp, 0);
64 		return TEEC_ERROR_BAD_PARAMETERS;
65 	}
66 
67 	*file_len = (uint32_t)(fp->f_inode->i_size);
68 
69 	ret = teek_open_app_file(fp, file_buf, *file_len);
70 	if (ret != TEEC_SUCCESS)
71 		tloge("do read app fail\n");
72 
73 	if (fp != NULL) {
74 		filp_close(fp, 0);
75 		fp = NULL;
76 	}
77 
78 	return ret;
79 }
80 
teek_free_app(bool load_app_flag,char ** file_buf)81 void teek_free_app(bool load_app_flag, char **file_buf)
82 {
83 	if (load_app_flag && file_buf != NULL && *file_buf != NULL) {
84 		vfree(*file_buf);
85 		*file_buf = NULL;
86 	}
87 }
88 
teek_get_app(const char * ta_path,char ** file_buf,uint32_t * file_len)89 int32_t teek_get_app(const char *ta_path, char **file_buf, uint32_t *file_len)
90 {
91 	int32_t ret;
92 
93 	/* ta path is NULL means no need to load TA */
94 	if (!ta_path)
95 		return TEEC_SUCCESS;
96 
97 	if (!file_buf || !file_len) {
98 		tloge("load app params invalied\n");
99 		return TEEC_ERROR_BAD_PARAMETERS;
100 	}
101 
102 	ret = teek_read_app(ta_path, file_buf, file_len);
103 	if (ret != TEEC_SUCCESS)
104 		tloge("teec load app error %d\n", ret);
105 
106 	return ret;
107 }
108