static int bt_cat(BT_HANDLE hShell, int argc, char **argv) { BT_HANDLE hStdout = BT_ShellGetStdout(hShell); BT_ERROR Error; char *szpPath = 0; if(argc == 1) { szpPath = "/"; } else if(argc == 2) { szpPath = argv[1]; } else { bt_fprintf(hStdout, "Usage: %s [path]\n", argv[0]); return -1; } BT_HANDLE hFile = BT_Open(szpPath, BT_GetModeFlags("rb"), &Error); if(!hFile) { bt_fprintf(hStdout, "cat: cannot access %s: No such file or not a file\n", szpPath); return 0; } BT_s32 c; while((c = BT_GetC(hFile, 0)) >= 0) { bt_fprintf(hStdout, "%c", c); } bt_fprintf(hStdout, "\n"); BT_CloseHandle(hFile); return 0; }
int main(void) { BT_HANDLE hUart = BT_Open("/dev/serial1", 0, NULL); BT_UART_CONFIG oConfig; BT_SetPowerState(hUart, BT_POWER_STATE_AWAKE); oConfig.eMode = BT_UART_MODE_POLLED; oConfig.ucDataBits = BT_UART_8_DATABITS; oConfig.ucStopBits = BT_UART_ONE_STOP_BIT; oConfig.ucParity = BT_UART_PARITY_NONE; oConfig.ulBaudrate = 115200; BT_UartSetConfiguration(hUart, &oConfig); BT_UartEnable(hUart); BT_SetStdout(hUart); BT_SetStdin(hUart); BT_HANDLE hKernel = BT_GetKernelProcessHandle(); BT_SetProcessFileDescriptor(hKernel, 1, hUart); BT_kPrint("ThunderCat booted:"); BT_kPrint("Starting init program"); BT_THREAD_CONFIG oTConfig; oTConfig.ulStackDepth = 1024; oTConfig.ulPriority = 0; oTConfig.ulFlags = 0; //test_case(0, 0); // BT_CreateProcess(thread, "pr-0", &oTConfig, NULL); // BT_CreateProcess(thread, "pr-1", &oTConfig, NULL); // BT_CreateProcess(thread, "pr-2", &oTConfig, NULL); // BT_CreateProcess(thread, "pr-3", &oTConfig, NULL); // BT_CreateThread(thread, &oTConfig, NULL); BT_CreateThread(test_case, &oTConfig, NULL); //BT_CreateThread(thread, &oTConfig, NULL); BT_HANDLE hShell = BT_ShellCreate(hUart, hUart, "ThunderCat>", 0, NULL); BT_Shell(hShell); while(1) { BT_ThreadSleep(1000); } }
static int bt_partition_disk(BT_HANDLE hShell, int argc, char **argv) { if(argc > 2) { usage_disk(hShell); return -1; } if(argc == 1) { BT_PRSHELL("Current disk: %s\n", device_path ? device_path : "none-selected"); return 0; } if(!device_path) { int retval = 0; BT_ERROR Error; BT_HANDLE hBlock = BT_Open(argv[1], 0, &Error); if(!hBlock) { BT_PRSHELL("Error: Could not open %s\n", argv[1]); return -1; } /** * Ensure we have a valid block device handle. * This API will return BT_ERR_INVALID_HANDLE, or BT_ERR_NONE. * * Maybe we should actually add a simple API for that! **/ if(BT_GetBlockGeometry(hBlock, NULL) != BT_ERR_NONE) { BT_PRSHELL("Error: %s is not a RAW block device\n", argv[1]); retval = -1; goto close_out; } device_path = BT_kMalloc(strlen(argv[1])); strcpy(device_path, argv[1]); close_out: BT_CloseHandle(hBlock); return retval; } else { BT_PRSHELL("Already working with: %s\n", device_path); } return 0; }
static int bt_partition_list(BT_HANDLE hShell, int argc, char **argv) { if(argc != 1) { usage_list(hShell); return -1; } if(!device_path) { return no_disk(hShell); } BT_BLOCK_GEOMETRY oGeom; BT_u32 i; if(!p_partition_info) { BT_ERROR Error; BT_HANDLE hBlock = BT_Open(device_path, 0, &Error); if(!hBlock) { BT_PRSHELL("Error: Could not open %s\n", argv[1]); return -1; } BT_GetBlockGeometry(hBlock, &oGeom); g_partitions = BT_PartitionCount(hBlock); for(i = 0; i < g_partitions; i++) { BT_PartitionInfo(hBlock, i, &g_partition_info[i]); } p_partition_info = g_partition_info; BT_CloseHandle(hBlock); } BT_PRSHELL("Device %s contains %d partitions\n", device_path, g_partitions); BT_PRSHELL("BlockSize : %lu\n", oGeom.ulBlockSize); BT_PRSHELL("Total Blocks : %lu\n", oGeom.ulTotalBlocks); for(i = 0; i < g_partitions; i++) { BT_PRSHELL(" %d : %lu : %lu\n", i, g_partition_info[i].ulStartLBA, g_partition_info[i].ulSectorCount); } return 0; }
BT_ERROR BT_ShellScript(const BT_i8 *path) { BT_ERROR Error; BT_HANDLE hFile = BT_Open(path, "rb", &Error); if(!hFile) { BT_kPrint("Could not open shell script %s\n", path); return BT_ERR_GENERIC; } BT_i8 *line = BT_kMalloc(256); if(!line) { BT_CloseHandle(hFile); return BT_ERR_NO_MEMORY; } BT_u32 linelen; while((linelen = BT_GetS(hFile, 256, line)) > 0) { BT_i8 *p = line; while(isspace((int) *p)) { p++; } if(*p == '#') { continue; // commented line! } if(p == (line + linelen)) { continue; } Error = BT_ShellCommand(p); } BT_kFree(line); BT_CloseHandle(hFile); return BT_ERR_NONE; }
static int bt_load_fpga(BT_HANDLE hShell, int argc, char **argv) { BT_HANDLE hStdout = BT_ShellGetStdout(hShell); BT_ERROR Error; if(argc != 4 && argc != 5) { bt_fprintf(hStdout, "Usage: %s [buffer_address] [fpga_device] [bitstream] [length]\n", argv[0]); return -1; } BT_u32 length = 0; BT_u32 addr = strtoul(argv[1], NULL, 16); void *p = (void *) addr; if(argv[3][0] == '-' && argc == 5) { // Image loaded and length provided. length = strtoul(argv[4], NULL, 10); goto flush; } BT_HANDLE hFile = BT_Open(argv[3], BT_GetModeFlags("rb"), &Error); if(!hFile) { bt_fprintf(hStdout, "Could not open bitstream at %s\n", argv[3]); return -1; } BT_HANDLE hInode = BT_GetInode(argv[3], &Error); if(!hInode) { bt_fprintf(hStdout, "Could not stat bitstream at %s\n", argv[3]); BT_CloseHandle(hFile); return -1; } BT_INODE oInode; BT_ReadInode(hInode, &oInode); BT_kPrint("Loading %s at %08X (%llu bytes)", argv[3], addr, oInode.ullFileSize); BT_Read(hFile, 0, oInode.ullFileSize, p, &Error); BT_kPrint("Load successful"); if(hFile) { BT_CloseHandle(hFile); } if(hInode) { BT_CloseHandle(hInode); } length = oInode.ullFileSize; flush: BT_DCacheFlush(); BT_HANDLE hFPGA = BT_DeviceOpen(argv[2], &Error); if(!hFPGA) { bt_printf("Failed to open fpga device %s\n", argv[2]); return -1; } BT_Write(hFPGA, 0, length, p, &Error); BT_CloseHandle(hFPGA); return 0; }