sec_mod.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Copyright (C) 2011-2014 MediaTek Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify it under the terms of the
  5. * GNU General Public License version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  8. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. * See the GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License along with this program.
  12. * If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /******************************************************************************
  15. * INCLUDE LINUX HEADER
  16. ******************************************************************************/
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/slab.h>
  22. #include <linux/unistd.h>
  23. #include <linux/sched.h>
  24. #include <linux/fs.h>
  25. #include <asm/uaccess.h>
  26. #include <linux/version.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/semaphore.h>
  29. #include <linux/delay.h>
  30. #include <linux/kthread.h>
  31. #include <linux/errno.h>
  32. #include <linux/cdev.h>
  33. #include <linux/kdev_t.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/device.h>
  36. #include <linux/mutex.h>
  37. #include <linux/vmalloc.h>
  38. #include <linux/poll.h>
  39. #include <linux/proc_fs.h>
  40. #include <linux/string.h>
  41. /* #include <mach/memory.h> */
  42. #include <asm/io.h>
  43. #include <linux/device.h>
  44. #include <linux/cdev.h>
  45. #include <linux/proc_fs.h>
  46. #include <linux/seq_file.h>
  47. #include <linux/interrupt.h>
  48. #include <linux/irq.h>
  49. #include <linux/of_platform.h>
  50. #include <linux/of_irq.h>
  51. #include <linux/of_address.h>
  52. #ifdef CONFIG_OF
  53. #include <linux/of_fdt.h>
  54. #endif
  55. /******************************************************************************
  56. * INCLUDE LIBRARY
  57. ******************************************************************************/
  58. #include "sec_osal.h"
  59. #include "sec_mod.h"
  60. #include "sec_boot_lib.h"
  61. #ifdef MTK_SECURITY_MODULE_LITE
  62. #include "masp_version.h"
  63. #endif
  64. #define SEC_DEV_NAME "sec"
  65. #define SEC_MAJOR 182
  66. #define MOD "MASP"
  67. #define TRACE_FUNC() MSG_FUNC(SEC_DEV_NAME)
  68. /*************************************************************************
  69. * GLOBAL VARIABLE
  70. **************************************************************************/
  71. static struct sec_mod sec = { 0 };
  72. static struct cdev sec_dev;
  73. static struct class *sec_class;
  74. static struct device *sec_device;
  75. #ifdef CONFIG_ARM64
  76. unsigned long long hacc_base;
  77. /*unsigned long long es_base;*/
  78. #else
  79. unsigned int hacc_base;
  80. /*unsigned int es_base;*/
  81. #endif
  82. static const struct of_device_id masp_of_ids[] = {
  83. {.compatible = "mediatek,hacc",},
  84. {}
  85. };
  86. #if 0
  87. /* ****************************
  88. * FOR ES_BASE ONLY
  89. ******************************/
  90. int es_probe(struct platform_device *dev)
  91. {
  92. #ifdef CONFIG_ARM64
  93. es_base = (unsigned long long)of_iomap(dev->dev.of_node, 0);
  94. #else
  95. es_base = (unsigned int)of_iomap(dev->dev.of_node, 0);
  96. #endif
  97. if (!es_base) {
  98. pr_err("[%s] ES register remapping failed\n", SEC_DEV_NAME);
  99. return -ENXIO;
  100. }
  101. return 0;
  102. }
  103. int es_remove(struct platform_device *dev)
  104. {
  105. es_base = 0;
  106. return 0;
  107. }
  108. static const struct of_device_id es_of_ids[] = {
  109. {.compatible = "mediatek,efusec",},
  110. {}
  111. };
  112. static struct platform_driver es_driver = {
  113. .driver = {
  114. .name = "es",
  115. .owner = THIS_MODULE,
  116. .of_match_table = es_of_ids,
  117. },
  118. .probe = es_probe,
  119. .remove = es_remove,
  120. };
  121. #endif
  122. /**************************************************************************
  123. * SEC DRIVER OPEN
  124. **************************************************************************/
  125. static int sec_open(struct inode *inode, struct file *file)
  126. {
  127. return 0;
  128. }
  129. /**************************************************************************
  130. * SEC DRIVER RELEASE
  131. **************************************************************************/
  132. static int sec_release(struct inode *inode, struct file *file)
  133. {
  134. return 0;
  135. }
  136. /**************************************************************************
  137. * SEC DRIVER IOCTL
  138. **************************************************************************/
  139. static long sec_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  140. {
  141. #ifdef MTK_SECURITY_MODULE_LITE
  142. return -EIO;
  143. #else
  144. return sec_core_ioctl(file, cmd, arg);
  145. #endif
  146. }
  147. static const struct file_operations sec_fops = {
  148. .owner = THIS_MODULE,
  149. .open = sec_open,
  150. .release = sec_release,
  151. .write = NULL,
  152. .read = NULL,
  153. .unlocked_ioctl = sec_ioctl
  154. };
  155. /**************************************************************************
  156. * SEC RID PROC FUNCTION
  157. **************************************************************************/
  158. static int sec_proc_rid_show(struct seq_file *m, void *v)
  159. {
  160. unsigned int rid[4] = { 0 };
  161. unsigned int i = 0;
  162. sec_get_random_id((unsigned int *)rid);
  163. for (i = 0; i < 16; i++)
  164. seq_putc(m, *((char *)rid + i));
  165. return 0;
  166. }
  167. static int sec_proc_rid_open(struct inode *inode, struct file *file)
  168. {
  169. return single_open(file, sec_proc_rid_show, NULL);
  170. }
  171. static const struct file_operations sec_proc_rid_fops = {
  172. .open = sec_proc_rid_open,
  173. .read = seq_read,
  174. .llseek = seq_lseek,
  175. .release = seq_release,
  176. };
  177. /**************************************************************************
  178. * SEC MODULE PARAMETER
  179. **************************************************************************/
  180. static uint recovery_done;
  181. module_param(recovery_done, uint, S_IRUSR | S_IWUSR /*|S_IWGRP */ | S_IRGRP | S_IROTH); /* rw-r--r-- */
  182. MODULE_PARM_DESC(recovery_done,
  183. "A recovery sync parameter under sysfs (0=complete, 1=on-going, 2=error)");
  184. /**************************************************************************
  185. * SEC DRIVER INIT
  186. **************************************************************************/
  187. static int sec_init(struct platform_device *dev)
  188. {
  189. int ret = 0;
  190. dev_t id;
  191. pr_debug("[%s] sec_init (%d)\n", SEC_DEV_NAME, ret);
  192. #ifdef CONFIG_ARM64
  193. hacc_base = (unsigned long long)of_iomap(dev->dev.of_node, 0);
  194. #else
  195. hacc_base = (unsigned int)of_iomap(dev->dev.of_node, 0);
  196. #endif
  197. if (!hacc_base) {
  198. pr_err("[%s] HACC register remapping failed\n", SEC_DEV_NAME);
  199. return -ENXIO;
  200. }
  201. id = MKDEV(SEC_MAJOR, 0);
  202. ret = register_chrdev_region(id, 1, SEC_DEV_NAME);
  203. if (ret) {
  204. pr_err("[%s] Regist Failed (%d)\n", SEC_DEV_NAME, ret);
  205. return ret;
  206. }
  207. sec_class = class_create(THIS_MODULE, SEC_DEV_NAME);
  208. if (NULL == sec_class) {
  209. pr_err("[%s] Create class failed(0x%x)\n", SEC_DEV_NAME, ret);
  210. ret = -1;
  211. return ret;
  212. }
  213. cdev_init(&sec_dev, &sec_fops);
  214. sec_dev.owner = THIS_MODULE;
  215. ret = cdev_add(&sec_dev, id, 1);
  216. if (ret < 0)
  217. goto exit;
  218. sec_device = device_create(sec_class, NULL, id, NULL, SEC_DEV_NAME);
  219. if (NULL == sec_class) {
  220. pr_err("[%s] Create device failed(0x%x)\n", SEC_DEV_NAME, ret);
  221. class_destroy(sec_class);
  222. ret = -1;
  223. return ret;
  224. }
  225. sec.id = id;
  226. sec.init = 1;
  227. spin_lock_init(&sec.lock);
  228. proc_create("rid", 0, NULL, &sec_proc_rid_fops);
  229. #ifdef MTK_SECURITY_MODULE_LITE
  230. pr_debug("[MASP Lite] version '%s%s', enter.\n", BUILD_TIME, BUILD_BRANCH);
  231. #endif
  232. exit:
  233. if (ret != 0) {
  234. device_destroy(sec_class, id);
  235. class_destroy(sec_class);
  236. unregister_chrdev_region(id, 1);
  237. memset(&sec, 0, sizeof(sec));
  238. }
  239. return ret;
  240. }
  241. /**************************************************************************
  242. * SEC DRIVER EXIT
  243. **************************************************************************/
  244. static void sec_exit(void)
  245. {
  246. remove_proc_entry("rid", NULL);
  247. cdev_del(&sec_dev);
  248. unregister_chrdev_region(sec.id, 1);
  249. memset(&sec, 0, sizeof(sec));
  250. #ifdef MTK_SECURITY_MODULE_LITE
  251. pr_debug("[MASP Lite] version '%s%s', exit.\n", BUILD_TIME, BUILD_BRANCH);
  252. #else
  253. sec_core_exit();
  254. #endif
  255. }
  256. /**************************************************************************
  257. * MASP PLATFORM DRIVER WRAPPER, FOR BUILD-IN SEQUENCE
  258. **************************************************************************/
  259. int masp_probe(struct platform_device *dev)
  260. {
  261. int ret = 0;
  262. ret = sec_init(dev);
  263. return ret;
  264. }
  265. int masp_remove(struct platform_device *dev)
  266. {
  267. sec_exit();
  268. return 0;
  269. }
  270. static struct platform_driver masp_driver = {
  271. .driver = {
  272. .name = "masp",
  273. .owner = THIS_MODULE,
  274. .of_match_table = masp_of_ids,
  275. },
  276. .probe = masp_probe,
  277. .remove = masp_remove,
  278. };
  279. static int __init masp_init(void)
  280. {
  281. int ret;
  282. #if 0
  283. ret = platform_driver_register(&es_driver);
  284. if (ret) {
  285. pr_err("[ES] Reg platform driver failed (%d)\n", ret);
  286. return ret;
  287. }
  288. #endif
  289. ret = platform_driver_register(&masp_driver);
  290. if (ret) {
  291. pr_err("[%s] Reg platform driver failed (%d)\n", SEC_DEV_NAME, ret);
  292. return ret;
  293. }
  294. return ret;
  295. }
  296. #ifdef CONFIG_OF
  297. static int __init masp_parse_dt(unsigned long node, const char *uname, int depth, void *data)
  298. {
  299. struct masp_tag *tags;
  300. int i;
  301. if (depth != 1 || (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  302. return 0;
  303. tags = (struct masp_tag *)of_get_flat_dt_prop(node, "atag,masp", NULL);
  304. if (tags) {
  305. g_rom_info_sbc_attr = tags->rom_info_sbc_attr;
  306. g_rom_info_sdl_attr = tags->rom_info_sdl_attr;
  307. g_hw_sbcen = tags->hw_sbcen;
  308. g_lock_state = tags->lock_state;
  309. lks = tags->lock_state;
  310. for (i = 0; i < NUM_RID; i++)
  311. g_random_id[i] = tags->rid[i];
  312. for (i = 0; i < NUM_CRYPTO_SEED; i++)
  313. g_crypto_seed[i] = tags->crypto_seed[i];
  314. for (i = 0; i < NUM_SBC_PUBK_HASH; i++)
  315. g_sbc_pubk_hash[i] = tags->sbc_pubk_hash[i];
  316. }
  317. return 1;
  318. }
  319. static int __init masp_of_init(void)
  320. {
  321. of_scan_flat_dt(masp_parse_dt, NULL);
  322. return 0;
  323. }
  324. #endif
  325. static void __exit masp_exit(void)
  326. {
  327. /*platform_driver_unregister(&es_driver);*/
  328. platform_driver_unregister(&masp_driver);
  329. }
  330. module_init(masp_init);
  331. module_exit(masp_exit);
  332. /**************************************************************************
  333. * EXPORT FUNCTION
  334. **************************************************************************/
  335. EXPORT_SYMBOL(sec_get_random_id);
  336. MODULE_LICENSE("GPL");
  337. MODULE_AUTHOR("MediaTek Inc.");
  338. #ifdef CONFIG_OF
  339. early_initcall(masp_of_init);
  340. #endif
  341. #ifdef MTK_SECURITY_MODULE_LITE
  342. MODULE_DESCRIPTION("Mediatek Security Module Lite");
  343. #else
  344. MODULE_DESCRIPTION("Mediatek Security Module");
  345. #endif