password_safe.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* Motenpoche
  2. *
  3. * (c) 2023 Daniele Lacamera <root@danielinux.net>
  4. *
  5. *
  6. * Motenpoche is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Motenpoche is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <stdint.h>
  23. #include "pico/stdlib.h"
  24. #include "pico/multicore.h"
  25. #include "hardware/gpio.h"
  26. #include "hardware/adc.h"
  27. #include "hardware/i2c.h"
  28. #include "hardware/spi.h"
  29. #include "cryptoengine.h"
  30. #include "bsp/board.h"
  31. #include "tusb.h"
  32. #include "usb_descriptors.h"
  33. #include "ui.h"
  34. #include "user_settings.h"
  35. #include "wolfssl/wolfcrypt/settings.h"
  36. #include "class/cdc/cdc.h"
  37. #include "class/cdc/cdc_device.h"
  38. #include "class/msc/msc.h"
  39. #include "hardware/gpio.h"
  40. #include "flash.h"
  41. #include "display.h"
  42. #include "hid.h"
  43. void core1_main(void);
  44. static volatile int main_plug = 1;
  45. int thread_control_ui = 0;
  46. /* DOUT */
  47. #define RED_LED 17
  48. #define GREEN_LED 16
  49. #define PICO_LED PICO_DEFAULT_LED_PIN
  50. static uint Led[] = {
  51. RED_LED,
  52. GREEN_LED,
  53. PICO_LED
  54. };
  55. /* DIN */
  56. #define ROT_S0 19
  57. #define ROT_S1 20
  58. #define ROT_KEY 18
  59. #define OK_BUTTON 21
  60. #define BACK_BUTTON 22
  61. /* I2C */
  62. #define I2C (&i2c1_inst)
  63. #define I2C_BAUDRATE 48000
  64. #define I2C_SCL_PIN 27
  65. #define I2C_SDA_PIN 26
  66. /* SPI */
  67. #define SPI (spi0)
  68. #define SPI_BAUDRATE 100000
  69. #define SPI_SCLK_PIN 2
  70. #define SPI_MOSI_PIN 3
  71. #define SPI_MISO_PIN 4
  72. #define SPI_FLASH_CS_PIN 5
  73. uint16_t flash_info = 0xFFFF;
  74. #define AIRCR *(volatile uint32_t *)(0xE000ED0C)
  75. #define AIRCR_VKEY (0x05FA << 16)
  76. #define AIRCR_SYSRESETREQ (1 << 2)
  77. static void sys_reboot(void)
  78. {
  79. AIRCR = AIRCR_SYSRESETREQ | AIRCR_VKEY;
  80. while(1)
  81. ;
  82. }
  83. void system_reboot(void)
  84. {
  85. int i;
  86. main_plug = 0;
  87. thread_control_ui = 2;
  88. for (i = display_getcontrast(); i >= 0; i--) {
  89. display_setcontrast(i);
  90. sleep_ms(5);
  91. }
  92. display_clear();
  93. }
  94. void system_boot(void)
  95. {
  96. int i, ret;
  97. set_sys_clock_48mhz();
  98. multicore_reset_core1();
  99. //stdio_init_all();
  100. printf("Initializing USB...\r\n");
  101. tusb_init();
  102. printf("Done.\r\n");
  103. /* LED and Douts */
  104. printf("Setting LEDs\r\n");
  105. for (i = 0; i < 3; i++) {
  106. gpio_init(Led[i]);
  107. gpio_set_dir(Led[i], GPIO_OUT);
  108. gpio_put(Led[i], 0);
  109. }
  110. gpio_put(RED_LED, 1);
  111. gpio_put(GREEN_LED, 1);
  112. printf("Done.\n");
  113. /* BTN Dins */
  114. printf("Setting buttons and digital inputs...\r\n");
  115. gpio_init(ROT_S0);
  116. gpio_init(ROT_S1);
  117. gpio_init(ROT_KEY);
  118. gpio_init(OK_BUTTON);
  119. gpio_init(BACK_BUTTON);
  120. gpio_set_dir(ROT_S0, GPIO_IN);
  121. gpio_set_dir(ROT_S1, GPIO_IN);
  122. gpio_set_dir(ROT_KEY, GPIO_IN);
  123. gpio_set_dir(OK_BUTTON, GPIO_IN);
  124. gpio_set_dir(BACK_BUTTON, GPIO_IN);
  125. gpio_pull_down(ROT_S0);
  126. gpio_pull_down(ROT_S1);
  127. gpio_pull_up(ROT_KEY);
  128. gpio_pull_down(OK_BUTTON);
  129. gpio_pull_down(BACK_BUTTON);
  130. printf("Done.\n");
  131. printf("Initializing SPI Flash\r\n");
  132. gpio_init(SPI_SCLK_PIN);
  133. gpio_init(SPI_MOSI_PIN);
  134. gpio_init(SPI_MISO_PIN);
  135. gpio_init(SPI_FLASH_CS_PIN);
  136. gpio_set_function(SPI_SCLK_PIN, GPIO_FUNC_SPI);
  137. gpio_set_function(SPI_MOSI_PIN, GPIO_FUNC_SPI);
  138. gpio_set_function(SPI_MISO_PIN, GPIO_FUNC_SPI);
  139. gpio_set_dir(SPI_FLASH_CS_PIN, GPIO_OUT);
  140. gpio_put(SPI_FLASH_CS_PIN, 1);
  141. spi_init(SPI, SPI_BAUDRATE);
  142. flash_info = flash_init(SPI);
  143. printf("Initializing I2C Display\r\n");
  144. gpio_init(I2C_SDA_PIN);
  145. gpio_init(I2C_SCL_PIN);
  146. gpio_set_function(I2C_SDA_PIN, GPIO_FUNC_I2C);
  147. gpio_set_function(I2C_SCL_PIN, GPIO_FUNC_I2C);
  148. i2c_init(I2C, I2C_BAUDRATE);
  149. ui_init(I2C);
  150. printf("Done\r\n");
  151. printf("Starting CPU Core 1\r\n");
  152. multicore_launch_core1(core1_main);
  153. printf("Done\r\n");
  154. gpio_put(RED_LED, 0);
  155. printf("Init complete.\r\n");
  156. }
  157. #define ROT_UP 1
  158. #define ROT_DOWN 2
  159. uint32_t rotary_action(void)
  160. {
  161. uint32_t action = 0;
  162. if (gpio_get(ROT_S0))
  163. action |= 1;
  164. if (gpio_get(ROT_S1))
  165. action |= 2;
  166. return action;
  167. }
  168. int rot_updown(void)
  169. {
  170. static int rot_up = 0;
  171. static int rot_down = 0;
  172. static uint32_t rotary_pos = 0;
  173. uint32_t rotary_now;
  174. rotary_now = rotary_action();
  175. if (rotary_now != rotary_pos) {
  176. if ((rotary_pos == 0 && rotary_now == 3) ||
  177. (rotary_pos == 3 && rotary_now == 2) ||
  178. (rotary_pos == 2 && rotary_now == 0) ||
  179. (rotary_pos == 1 && rotary_now == 3)) {
  180. rot_down++;
  181. rot_up = 0;
  182. }
  183. if ((rotary_pos == 0 && rotary_now == 1) ||
  184. (rotary_pos == 3 && rotary_now == 1) ||
  185. (rotary_pos == 2 && rotary_now == 3) ||
  186. (rotary_pos == 1 && rotary_now == 0)) {
  187. rot_up++;
  188. rot_down = 0;
  189. }
  190. rotary_pos = rotary_now;
  191. }
  192. if (rot_up > 1) {
  193. rot_up = 0;
  194. return ROT_UP;
  195. }
  196. if (rot_down > 1) {
  197. rot_down = 0;
  198. return ROT_DOWN;
  199. }
  200. return 0;
  201. }
  202. static uint32_t key_press_counter = 0;
  203. static uint32_t ok_press_counter = 0;
  204. static uint32_t back_press_counter = 0;
  205. void clear_input(void)
  206. {
  207. key_press_counter = 0;
  208. ok_press_counter = 0;
  209. back_press_counter = 0;
  210. }
  211. void poll_buttons(void)
  212. {
  213. int rot_ud = 0;
  214. if (gpio_get(ROT_KEY) == 0) {
  215. key_press_counter++;
  216. } else {
  217. key_press_counter = 0;
  218. }
  219. if (gpio_get(OK_BUTTON) != 0) {
  220. ok_press_counter++;
  221. } else {
  222. ok_press_counter = 0;
  223. }
  224. if (gpio_get(BACK_BUTTON) != 0) {
  225. back_press_counter++;
  226. } else {
  227. back_press_counter = 0;
  228. }
  229. if (ok_press_counter == 100) {
  230. printf("OK: pressed\n");
  231. ui_confirm_button();
  232. return;
  233. }
  234. if (key_press_counter == 100) {
  235. printf("key: pressed\n");
  236. ui_key_button();
  237. return;
  238. }
  239. if (back_press_counter == 100) {
  240. printf("back: pressed\n");
  241. ui_back_button();
  242. return;
  243. }
  244. if ((fsm_get() == VAULT_BOOTUP) && (back_press_counter == 100000)) {
  245. uint32_t i;
  246. for (i = 0; i < VAULT_FLASH_SIZE; i+=SPI_FLASH_SECTOR_SIZE)
  247. flash_sector_erase(i);
  248. system_reboot();
  249. return;
  250. }
  251. rot_ud = rot_updown();
  252. if (rot_ud == ROT_UP) {
  253. ui_rot_up();
  254. sleep_ms(1);
  255. return;
  256. }
  257. if (rot_ud == ROT_DOWN) {
  258. ui_rot_down();
  259. sleep_ms(1);
  260. return;
  261. }
  262. }
  263. uint8_t *fbuf = NULL;
  264. void tud_mount_cb(void)
  265. {
  266. gpio_put(RED_LED, 0);
  267. }
  268. void tud_umount_cb(void)
  269. {
  270. gpio_put(RED_LED, 1);
  271. }
  272. static uint8_t flash_buf[4];
  273. extern void msc_task(void);
  274. int main(void) {
  275. int i;
  276. system_boot();
  277. printf("Loop started.\n");
  278. while (main_plug) {
  279. uint32_t ms_tick = board_millis();
  280. if ((ms_tick % 10) == 0) {
  281. if (thread_control_ui == 0) {
  282. poll_buttons();
  283. }
  284. }
  285. if (thread_control_ui == 0) {
  286. if ((ms_tick % 100) == 0) {
  287. ui_task();
  288. }
  289. }
  290. msc_task();
  291. }
  292. /* End of session. Goodbye. */
  293. multicore_reset_core1();
  294. sys_reboot();
  295. }
  296. void core1_main(void)
  297. {
  298. while(1) {
  299. hid_task();
  300. tud_task();
  301. }
  302. }