00001 #include <buffer.h>
00002 #include <debug.h>
00003 #include <dmapool.h>
00004 #include <interrupt.h>
00005 #include <watchdog.h>
00006 #include <workqueue.h>
00007 #include <timer_tc.h>
00008 #include <board/init.h>
00009 #include <chip/memory-map.h>
00010 #include <chip/clk.h>
00011 #include <dmac/dmaca.h>
00012 #include <sdmmc/sdmmc.h>
00013 #include <sdmmc/mcihost.h>
00014 #include <block/device.h>
00015 #include <block/sdmmc.h>
00016 #include <block/aesblk.h>
00017 #include <usb/udc.h>
00018 #include <usb/function_core.h>
00019 #include <usb/msc_function.h>
00020 #include <usb/request.h>
00021 #include <usb/usb_protocol.h>
00022 #include <status-codes.h>
00023 #include <malloc.h>
00024 #include <string.h>
00025 #include <aes.h>
00026 #include <atomic.h>
00027
00028 #include <app/usb_strings.h>
00029
00030
00031 #define MAX_SERIAL_NUMBER_LEN 13
00032
00033 const char *usb_dev_string_table[] = {
00034 [USB_STRING_LANGID] = "",
00035 [USB_STRING_DEV_MANUFACTURER] = "Atmel",
00036 [USB_STRING_DEV_PRODUCT] = "USB Mass Storage",
00037 #ifndef CONFIG_CPU_AT32AP7200
00038 [USB_STRING_DEV_SERIAL] = (const char *)FLASH_USER_PAGE_BASE,
00039 #endif
00040 };
00041
00042 static struct usb_configuration_descriptor config_desc = {
00043 .bLength = sizeof(struct usb_configuration_descriptor),
00044 .bDescriptorType = USB_DT_CONFIGURATION,
00045 .bNumInterfaces = 1,
00046 .bConfigurationValue = 1,
00047 .bmAttributes = 0xc0,
00048 .bMaxPower = 2,
00049 };
00050
00051 struct timer main_timer;
00052 struct workqueue main_workqueue;
00053
00054 const char *get_serial_number(void)
00055 {
00056 return (const char *)FLASH_USER_PAGE_BASE;
00057 }
00058
00059 static void card_event(struct block_device *bdev, void *data)
00060 {
00061 #ifdef CONFIG_USE_AES
00062 struct aes_block_device *aes_bdev = data;
00063 struct udc *udc = aes_bdev->udc;
00064 #else
00065 struct udc *udc = data;
00066 #endif
00067
00068 if (!bdev) {
00069 udc_detach(udc);
00070 } else if (!test_bit(BDEV_PRESENT, &bdev->flags)) {
00071 udc_detach(udc);
00072 } else if (!test_bit(BDEV_WRITEABLE, &bdev->flags)) {
00073 udc_detach(udc);
00074 } else {
00075 #ifdef CONFIG_USE_AES
00076 aesblk_update(aes_bdev);
00077 #endif
00078 udc_attach(udc);
00079 }
00080 }
00081
00082 int main(void)
00083 {
00084 struct udc *udc;
00085 struct usb_func_iface *msc_iface;
00086 struct usb_func_config *config;
00087 struct sdmmc_host *sdmmc_host;
00088 struct sdmmc_slot *slot;
00089 struct block_device *bdev;
00090
00091 #ifdef CONFIG_USE_AES
00092 struct aes_block_device aes_bdev;
00093 uint32_t cipher_key[8];
00094 uint32_t init_vector[4];
00095
00096 cipher_key[0] = 0x603deb10;
00097 cipher_key[1] = 0x15ca71be;
00098 cipher_key[2] = 0x2b73aef0;
00099 cipher_key[3] = 0x857d7781;
00100 cipher_key[4] = 0x1f352c07;
00101 cipher_key[5] = 0x3b6108d7;
00102 cipher_key[6] = 0x2d9810a3;
00103 cipher_key[7] = 0x0914dff4;
00104
00105 init_vector[0] = 0x00010203;
00106 init_vector[1] = 0x04050607;
00107 init_vector[2] = 0x08090a0b;
00108 init_vector[3] = 0x0c0d0e0f;
00109 #endif
00110
00111 cpu_irq_enable();
00112
00113
00114 pm_init_pll(0, PM_PLL_SRC_OSC0, 1, 11, PM_PLL_OUTPUT_DIV_BY_2);
00115 pm_enable_pll_sync(0);
00116 pm_set_main_clock(PM_MAINCLK_SRC_PLL0);
00117
00118 board_init();
00119
00120 dbg_init();
00121
00122 #ifdef CONFIG_USE_AES
00123 dbg_info("USB Mass Storage device with AES running\n");
00124 #else
00125 dbg_info("USB Mass Storage device running\n");
00126 #endif
00127
00128 dma_pool_init();
00129 buffer_pool_init();
00130 dmaca_init();
00131 usb_init();
00132
00133 timer_init(&main_timer, (void *)TC0_BASE, 1000);
00134 workqueue_init(&main_workqueue);
00135
00136 udc = udc_init();
00137 if (!udc) {
00138 dbg_panic("UDC initialization failed\n");
00139 return 1;
00140 }
00141
00142 if (!msc_serial_number_is_valid(
00143 usb_dev_string_table[USB_STRING_DEV_SERIAL],
00144 MAX_SERIAL_NUMBER_LEN)) {
00145 dbg_panic("Invalid serial number\n");
00146 return 1;
00147 }
00148
00149 config = usb_func_add_config(&config_desc);
00150
00151 sdmmc_host = sdmmc_mcihost_init();
00152 if (!sdmmc_host) {
00153 dbg_panic("Failed to initialize MMC host controller\n");
00154 return 1;
00155 }
00156
00157 slot = sdmmc_host->get_slot(sdmmc_host, 0);
00158 if (!slot) {
00159 dbg_panic("Could not get SDMMC slot\n");
00160 return 1;
00161 }
00162
00163 #ifdef CONFIG_USE_AES
00164 aes_bdev.udc = udc;
00165
00166 bdev = sdmmc_blkdev_init_new(slot, card_event, &aes_bdev);
00167 #else
00168 bdev = sdmmc_blkdev_init_new(slot, card_event, udc);
00169 #endif
00170 if (!bdev) {
00171 dbg_panic("Could not initialize block device\n");
00172 return 1;
00173 }
00174
00175 sdmmc_host_enable(sdmmc_host);
00176
00177 #ifdef CONFIG_USE_AES
00178 aes_init(&aes_bdev.module, AES_KEYSIZE_128 | AES_OPMODE_ECB);
00179 aes_load_key(&aes_bdev.module, cipher_key);
00180
00181 aesblk_init(&aes_bdev, bdev);
00182
00183
00184
00185
00186
00187 msc_iface = usb_msc_func_init(&aes_bdev.bdev);
00188 #else
00189 msc_iface = usb_msc_func_init(bdev);
00190 #endif
00191 if (!msc_iface) {
00192 dbg_panic("Failed to initialize MSC interface\n");
00193 return 1;
00194 }
00195
00196 watchdog_set_timeout(100);
00197 watchdog_enable();
00198
00199 usb_func_add_interface(config, msc_iface);
00200
00201 for (;;) {
00202 watchdog_reset();
00203 workqueue_do_one_item(&main_workqueue);
00204 }
00205
00206 return 0;
00207 }