static int bt_setenv(int argc, char **argv) { BT_u32 total_length = 0; BT_u32 i; for(i = 2; i < argc; i++) { total_length += strlen(argv[i]) + 1; // Add a space after each item. } if(!total_length) { clear_env(argv[1]); return 0; } char *s = BT_kMalloc(total_length+1); if(!s) { bt_printf("No memory could not allocated temporary memory to construct string\n"); return -1; } strcpy(s, ""); for(i = 2; i < argc; i++) { strcat(s, argv[i]); if(i == argc-1) { break; } strcat(s, " "); } BT_ShellSetEnv(argv[1], s, BT_ENV_T_STRING); BT_kFree(s); return 0; }
static BT_HANDLE fullfat_mount(BT_HANDLE hFS, BT_HANDLE hVolume, BT_ERROR *pError) { FF_ERROR ffError; BT_ERROR Error = BT_ERR_GENERIC; BT_FF_MOUNT *pMount = (BT_FF_MOUNT *) BT_CreateHandle(&oHandleInterface, sizeof(BT_FF_MOUNT), pError); if(!pMount) { return NULL; } pMount->pBlockCache = BT_kMalloc(8192); pMount->hVolume = hVolume; pMount->pIoman = FF_CreateIOMAN(pMount->pBlockCache, 8192, 512, &ffError); if(!pMount->pIoman) { Error = BT_ERR_GENERIC; goto err_free_out; } ffError = FF_RegisterBlkDevice(pMount->pIoman, 512, fullfat_writeblocks, fullfat_readblocks, pMount); ffError = FF_MountPartition(pMount->pIoman, 0); return (BT_HANDLE) pMount; err_free_out: BT_kFree(pMount->pBlockCache); BT_DestroyHandle((BT_HANDLE)pMount); *pError = Error; return NULL; }
static BT_HANDLE ext2_opendir(BT_HANDLE hMount, const BT_i8 *szpPath, BT_ERROR *pError) { if(!hMount) { if(pError) *pError = BT_ERR_GENERIC; goto err_out; } BT_EXT2_DIR *pDir = (BT_EXT2_DIR *) BT_CreateHandle(&oDirHandleInterface, sizeof(BT_EXT2_DIR), pError); if(!pDir) { *pError = BT_ERR_NO_MEMORY; goto err_out; } pDir->pMount = (BT_EXT2_MOUNT *)hMount; pDir->szpFname = BT_kMalloc(BT_EXT2_FNAME_MAX_STRLEN); if(!pDir->szpFname) { *pError = BT_ERR_NO_MEMORY; goto err_free_out; } if(ext2fs_open_dir((char *)szpPath) < 0) { if(pError) *pError = BT_ERR_GENERIC; goto err_free_out; } #if 0 if(ext2fs_ls((char *)szpPath) < 0) { if(pError) *pError = BT_ERR_GENERIC; goto err_free_out; } #endif return (BT_HANDLE) pDir; err_free_out: if(pDir->szpFname) BT_kFree(pDir->szpFname); BT_DestroyHandle((BT_HANDLE)pDir); err_out: return NULL; }
void *BT_kRealloc(void *p, BT_u32 ulSize) { void *n = NULL; // CRITICAL SECTION BT_kEnterCritical(); { if((p) && (ulSize)) { n = BT_kMalloc(ulSize); if (n) { BT_HEAP_BLOCK * pOld = (BT_HEAP_BLOCK *)p; pOld--; pOld->ulSize -= sizeof(BT_HEAP_BLOCK); if (ulSize > pOld->ulSize) memcpy(n, p, pOld->ulSize); else memcpy(n, p, ulSize); } } else if((p) && (!ulSize)) { BT_kFree(p); } else if((!p) && (ulSize)) { n = BT_kMalloc(ulSize); } } BT_kExitCritical(); return n; }
static BT_ERROR ext2_dir_cleanup(BT_HANDLE hDir) { ext2fs_close_dir(); if(hDir) { BT_EXT2_DIR *pDir = (BT_EXT2_DIR *)hDir; if(pDir->szpFname) BT_kFree(pDir->szpFname); } return BT_ERR_NONE; }
static int bt_partition_close(BT_HANDLE hShell, int argc, char **argv) { if(argc != 1) { usage_close(hShell); return -1; } if(device_path) { BT_kFree(device_path); device_path = NULL; p_partition_info = NULL; g_partitions = 0; } else { return no_disk(hShell); // Error message for no active disk. } return 0; }
static int fdt_set(BT_HANDLE hShell, int argc, char **argv) { BT_HANDLE hStdout = BT_ShellGetStdout(hShell); int retval = 0; if(argc < 2) { do_usage(hStdout, argv[0]); return -1; } char *path = argv[0]; char *property = argv[1]; char *data = NULL; int nodeoffset; int len = 0; nodeoffset = fdt_path_offset(g_fdt_addr, path); if(nodeoffset < 0) { bt_fprintf(hStdout, "libfdt: fdt_path_offset() returned %s\n", fdt_strerror(nodeoffset)); return -1; } if(argc == 3) { // Parse and allocate data as required. } int bAllocated = 0; retval = fdt_parse_property(hStdout, argv+2, argc-2, &data, &len, &bAllocated); if(retval < 0) { bt_fprintf(hStdout, "Error parsing property value string!\n"); goto err_out; } fdt_setprop(g_fdt_addr, nodeoffset, property, data, len); err_out: if(bAllocated) { BT_kFree(data); } return retval; }
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_ps(BT_HANDLE hShell, int argc, char **argv) { BT_HANDLE hStdout = BT_ShellGetStdout(hShell); BT_u32 i = 0; struct bt_process_time oTime; BT_u32 total_processes = BT_GetTotalProcesses(); struct process_time *runtimes = BT_kMalloc(sizeof(struct process_time) * total_processes); if(!runtimes) { return -1; } BT_u64 runtime = BT_GetGlobalTimer(); for(i = 0; i < total_processes; i++) { BT_GetProcessTime(&oTime, i); runtimes[i].ullRuntimeCounter = oTime.ullRunTimeCounter; } BT_ThreadSleep(1000); runtime = BT_GetGlobalTimer() - runtime; for(i = 0; i < total_processes; i++) { BT_GetProcessTime(&oTime, i); runtimes[i].ullRuntimeCounter = oTime.ullRunTimeCounter - runtimes[i].ullRuntimeCounter; } for(i = 0; i < total_processes; i++) { BT_GetProcessTime(&oTime, i); bt_fprintf(hStdout, "%s : %d%%\n", oTime.name, runtimes[i].ullRuntimeCounter / (runtime / 100)); } bt_fprintf(hStdout, "Total runtime %d seconds\n", (BT_u32) (BT_GetGlobalTimer() / (BT_u64)BT_GetGlobalTimerRate())); BT_kFree(runtimes); return 0; }
void *BT_kRealloc(void *p, BT_u32 ulSize) { void *n = NULL; if((p) && (ulSize)) { n = BT_kMalloc(ulSize); if (n) { struct MEM_TAG *tag = (struct MEM_TAG *) p; tag -= 1; if (ulSize > tag->size) memcpy(n, p, tag->size); else memcpy(n, p, ulSize); } } else if((p) && (!ulSize)) { BT_kFree(p); } else if((!p) && (ulSize)) { n = BT_kMalloc(ulSize); } return n; }
BT_ERROR BT_DestroyHandle(BT_HANDLE h) { BT_kFree(h); return BT_ERR_NONE; }
static BT_HANDLE timer_probe(const BT_INTEGRATED_DEVICE *pDevice, BT_ERROR *pError) { BT_ERROR Error = BT_ERR_NONE; BT_HANDLE hTimer = NULL; const BT_RESOURCE *pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_ENUM, 0); if(!pResource) { Error = BT_ERR_NO_MEMORY; goto err_free_out; } if (g_TIMER_HANDLES[pResource->ulStart]){ Error = BT_ERR_GENERIC; goto err_free_out; } hTimer = BT_CreateHandle(&oHandleInterface, sizeof(struct _BT_OPAQUE_HANDLE), pError); if(!hTimer) { goto err_out; } g_TIMER_HANDLES[pResource->ulStart] = hTimer; hTimer->pDevice = pDevice; pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_MEM, 0); if(!pResource) { Error = BT_ERR_NO_MEMORY; goto err_free_out; } hTimer->pRegs = (LM3Sxx_TIMER_REGS *) pResource->ulStart; TimerSetPowerState(hTimer, BT_POWER_STATE_AWAKE); ResetTimer(hTimer); pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_IRQ, 0); if(!pResource) { Error = BT_ERR_GENERIC; goto err_free_out; } /* On NVIC we don't need to register interrupts, LINKER has patched vector for us * Error = BT_RegisterInterrupt(pResource->ulStart, timer_irq_handler, hTimer); if(Error) { goto err_free_out; }*/ Error = BT_EnableInterrupt(pResource->ulStart); return hTimer; err_free_out: BT_kFree(hTimer); err_out: if(pError) { *pError = Error; } return NULL; }
BT_ERROR BT_ShellCommand(char *input) { BT_u32 ulArguments = 0; BT_u32 bIsArg = BT_FALSE; char *copy = BT_kMalloc(strlen(input)+1); if(!copy) { return BT_ERR_NO_MEMORY; } strcpy(copy, input); input = copy; while(isspace((int)*input)) { input++; // Eat up prefixed whitespace. } bIsArg = BT_TRUE; char *line = input; while(*input) { if(bIsArg) { if(isspace((int)*input)) { ulArguments += 1; bIsArg = BT_FALSE; } } else { if(!isspace((int)*input)) { bIsArg = BT_TRUE; } } input++; } if(bIsArg) { ulArguments += 1; } char **pargs = BT_kMalloc(sizeof(char *) * ulArguments); if(!pargs) { return BT_ERR_NO_MEMORY; } input = line; while(!*input); bIsArg = BT_FALSE; BT_u32 i = 0; while(*input) { if(!bIsArg) { if(!isspace((int)*input)) { bIsArg = BT_TRUE; pargs[i++] = input; } } else { if(isspace((int)*input)) { *input = '\0'; bIsArg = BT_FALSE; } } input++; } const BT_SHELL_COMMAND *pCommand = GetShellCommand(pargs[0]); if(!pCommand) { BT_kFree(pargs); BT_kFree(copy); return BT_ERR_NONE; } pCommand->pfnCommand(ulArguments, pargs); BT_kFree(pargs); BT_kFree(copy); return BT_ERR_NONE; }
void vPortFree(void *p) { BT_kFree(p); }
void free(void *p) { BT_kFree(p); }
static BT_HANDLE mac_probe(const BT_INTEGRATED_DEVICE *pDevice, BT_ERROR *pError) { BT_ERROR Error = BT_ERR_NONE; BT_HANDLE hMAC = NULL; const BT_RESOURCE *pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_ENUM, 0); if(!pResource) { Error = BT_ERR_NO_MEMORY; goto err_free_out; } if (g_mac_HANDLES[pResource->ulStart]){ Error = BT_ERR_GENERIC; goto err_free_out; } hMAC = BT_CreateHandle(&oHandleInterface, sizeof(struct _BT_OPAQUE_HANDLE), pError); if(!hMAC) { goto err_out; } g_mac_HANDLES[pResource->ulStart] = hMAC; hMAC->pDevice = pDevice; pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_MEM, 0); if(!pResource) { Error = BT_ERR_NO_MEMORY; goto err_free_out; } hMAC->pRegs = (LM3Sxx_MAC_REGS *) pResource->ulStart; macSetPowerState(hMAC, BT_POWER_STATE_AWAKE); pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_IRQ, 0); if(!pResource) { Error = BT_ERR_GENERIC; goto err_free_out; } /* On NVIC we don't need to register interrupts, LINKER has patched vector for us * Error = BT_RegisterInterrupt(pResource->ulStart, mac_irq_handler, hMAC); if(Error) { goto err_free_out; }*/ BT_SetInterruptPriority(pResource->ulStart, ((0x01 << BT_CONFIG_MACH_PRIORITY_BITS)-1)); Error = BT_EnableInterrupt(pResource->ulStart); Error = BT_RegisterNetworkInterface(hMAC); return hMAC; err_free_out: BT_kFree(hMAC); err_out: if(pError) { *pError = Error; } return NULL; }