void testCharDeviceIo(const wchar_t *name) { handle_t file; fileop_t op; uint32_t key; file = FsOpen(name, FILE_READ); if (file == NULL) wprintf(L"Failed to open %s\n", name); else { op.event = EvtCreate(false); op.bytes = 0; wprintf(L"op = %p op.event = %u sig = %u\n", &op, op.event, EvtIsSignalled(op.event)); if (op.event != NULL) { while (true) { status_t ret; ret = FsReadAsync(file, &key, 0, sizeof(key), &op); if (ret > 0) break; else if (ret == SIOPENDING) { DbgWrite(L"Wait start\n", 11); /*while (EvtIsSignalled(op.event))*/ ThrWaitHandle(op.event); DbgWrite(L"Wait end\n", 9); ret = op.result; } /*wprintf(L"op.result = %d op.bytes = %u\n", op.result, op.bytes);*/ if (ret == SIOPENDING) wprintf(L"op.result is still SIOPENDING\n"); else { if (ret > 0 || op.bytes == 0) break; wprintf(L"%c", (wchar_t) key); if (key == 27) break; } op.bytes = 0; } DbgWrite(L"Finished\n", 9); wprintf(L"Finished: op.result = %d, op.bytes = %u\n", op.result, op.bytes); HndClose(op.event); } } HndClose(file); }
bool FsWrite(handle_t file, const void *buf, uint64_t offset, size_t bytes, size_t *bytes_written) { fileop_t op; status_t ret; op.event = FsAllocSyncIoEvent(); ret = FsWriteAsync(file, buf, offset, bytes, &op); if (ret > 0) { FsFreeSyncIoEvent(op.event); errno = ret; return false; } else if (ret == SIOPENDING) { ThrWaitHandle(op.event); ret = op.result; } FsFreeSyncIoEvent(op.event); if (bytes_written != 0) *bytes_written = op.bytes; if (ret != 0) { errno = ret; return false; } else return true; }
bool FsRead(handle_t file, void *buf, uint64_t offset, size_t bytes, size_t *bytes_read) { fileop_t op; status_t ret; op.event = FsAllocSyncIoEvent(); ret = FsReadAsync(file, buf, offset, bytes, &op); if (ret > 0) { FsFreeSyncIoEvent(op.event); errno = ret; return false; } else if (ret == SIOPENDING) { if (!ThrWaitHandle(op.event)) DbgWrite(L"FsRead: ThrWaitHandle failed\n", 21); ret = op.result; } FsFreeSyncIoEvent(op.event); if (bytes_read != 0) *bytes_read = op.bytes; if (ret != 0) { if (ret == SIOPENDING) DbgWrite(L"FsRead: still pending\n", 22); errno = ret; return false; } else return true; }
bool FsRequestSync(handle_t file, uint32_t code, void *buf, size_t bytes, size_t *bytes_out) { fileop_t op; op.event = FsAllocSyncIoEvent(); if (!FsRequest(file, code, buf, bytes, &op)) { FsFreeSyncIoEvent(op.event); errno = op.result; return false; } while (op.result == SIOPENDING) ThrWaitHandle(op.event); FsFreeSyncIoEvent(op.event); if (op.result != 0) { errno = op.result; return false; } if (bytes_out != 0) *bytes_out = op.bytes; return true; }
static void EthHandleBinding(void) { net_binding_t *bind; request_eth_t req_eth; union { uint8_t raw[ETH_FRAME_LEN]; net_ether header; } packet; page_array_t *pages; io_callback_t cb; event_t *event; net_hwaddr_t src, dst; bind = ThrGetCurrent()->info->param; pages = MemCreatePageArray(packet.raw, sizeof(packet.raw)); event = EvtCreate(false); src.type = dst.type = NET_HW_ETHERNET; src.data_size = dst.data_size = 6; while (true) { req_eth.header.code = ETH_RECEIVE; req_eth.header.param = (void*) event; memcpy(req_eth.params.eth_receive.from, bcast, sizeof(bcast)); req_eth.params.eth_receive.type = (uint16_t) (addr_t) bind->hw_cookie; req_eth.params.eth_receive.pages = pages; req_eth.params.eth_receive.length = sizeof(packet.raw); cb.type = IO_CALLBACK_FUNCTION; cb.u.function = EthHandleBindingCompletion; if (!IoRequest(&cb, bind->dev, &req_eth.header)) { wprintf(L"EthHandleBinding: IoRequest error\n"); break; } ThrWaitHandle(ThrGetCurrent(), &event->hdr); KeYield(); memcpy(src.u.ethernet, packet.header.src, sizeof(src.u.ethernet)); memcpy(dst.u.ethernet, packet.header.dst, sizeof(dst.u.ethernet)); if (bind->proto->vtbl->receive_packet != NULL) bind->proto->vtbl->receive_packet(bind->proto, bind, &src, &dst, packet.raw + sizeof(packet.header), req_eth.params.eth_receive.length - sizeof(packet.header)); } HndClose(&event->hdr); ThrExitThread(0); KeYield(); }
void testFileIo(const wchar_t *name) { handle_t file; unsigned i; size_t len; fileop_t op; uint64_t offset; file = FsOpen(name, FILE_READ); if (file == NULL) { wprintf(L"Failed to open %s\n", name); return; } op.event = EvtCreate(false); for (i = 0; i < 16; i++) { offset = 0; wprintf(L"\x1b[%um", (i % 8) + 30); while (true) { status_t ret; ret = FsReadAsync(file, key, offset, sizeof(key), &op); if (ret > 0) break; else if (ret == SIOPENDING) { ThrWaitHandle(op.event); ret = op.result; } len = op.bytes; if (len == 0) break; if (len < sizeof(key)) key[len] = '\0'; len = mbstowcs(str, key, _countof(key) - 1); if (len == -1) wprintf(L"invalid multibyte sequence\n"); else _cputws(str, len); offset += op.bytes; } ThrSleep(5000); } HndClose(op.event); HndClose(file); }
void testBlockDeviceIo(const wchar_t *name) { handle_t file; unsigned i, j; fileop_t op; wchar_t *ch; file = FsOpen(name, FILE_READ); if (file == NULL) wprintf(L"Failed to open %s\n", name); else { op.event = EvtCreate(false); for (j = 0; j < 1; j++) { status_t ret; wprintf(/*L"\x1b[2J"*/ L"\x1b[%um", (j % 8) + 30); ret = FsReadAsync(file, key, 9 * 512, sizeof(key), &op); if (ret > 0) break; else if (ret == SIOPENDING) { ThrWaitHandle(op.event); ret = op.result; } if (ret == SIOPENDING) wprintf(L"op.result is still SIOPENDING\n"); else if (ret == 0 && op.bytes > 0) { wprintf(L"Read %u bytes\n", op.bytes); ch = str; for (i = 0; i < op.bytes; i++) { swprintf(ch, L"%02X", key[i]); ch += 2; } _cputws(str, op.bytes * 2); /*for (i = 0; i < op.bytes; i++) wprintf(L"%02X", key[i]);*/ } else break; } DbgWrite(L"Finished\n", 9); wprintf(L"Finished: op.result = %d, op.bytes = %u\n", op.result, op.bytes); HndClose(op.event); } HndClose(file); }
static void DevCallLoader(load_request_t *load) { event_t *evt_done; evt_done = EvtCreate(false); load->evt_done = evt_done; SemDown(mux_load); LIST_ADD(load, load); SemUp(mux_load); wprintf(L"DevCallLoader: submitting request\n"); SemUp(sem_load); ThrWaitHandle(current(), &evt_done->hdr); KeYield(); HndClose(&evt_done->hdr); }
wchar_t _wgetch(void) { static handle_t keyb, keyb_event; fileop_t op; uint32_t key; if (keyb == NULL) { keyb = FsOpen(SYS_DEVICES L"/keyboard", FILE_READ); if (keyb == NULL) return (wchar_t) -1; } if (keyb_event == NULL) { keyb_event = EvtCreate(false); if (keyb_event == NULL) return (wchar_t) -1; } op.event = keyb_event; do { status_t ret; ret = FsReadAsync(keyb, &key, 0, sizeof(key), &op); if (ret > 0) return false; else if (ret == SIOPENDING) { ThrWaitHandle(op.event); ret = op.result; } if (ret != 0 || op.bytes == 0) return -1; } while ((wchar_t) key == 0); return (wchar_t) key; }
void ShCmdV86(const wchar_t *cmd, wchar_t *params) { uint8_t *code; psp_t *psp; FARPTR fp_code, fp_stackend; handle_t thr, file; dirent_standard_t di; bool doWait; wchar_t **names, **values; ShParseParams(params, &names, &values, ¶ms); params = ShPrompt(L" .COM file? ", params); if (*params == '\0') return; doWait = true; if (ShHasParam(names, L"nowait")) doWait = false; free(names); free(values); /*if (!FsQueryFile(params, FILE_QUERY_STANDARD, &di, sizeof(di))) { _pwerror(params); return; }*/ di.length = 0x10000; file = FsOpen(params, FILE_READ); if (file == NULL) { wprintf(L"FsOpen: "); _pwerror(params); return; } code = aligned_alloc(di.length + 0x100); psp = (psp_t*) code; memset(psp, 0, sizeof(*psp)); psp->int20 = 0x20cd; if (!FsRead(file, code + 0x100, 0, di.length, NULL)) { wprintf(L"FsRead: "); _pwerror(params); HndClose(file); return; } HndClose(file); fp_code = i386LinearToFp(code); fp_code = MK_FP(FP_SEG(fp_code), FP_OFF(fp_code) + 0x100); if (sh_v86stack == NULL) sh_v86stack = aligned_alloc(65536); fp_stackend = i386LinearToFp(sh_v86stack); memset(sh_v86stack, 0, 65536); thr = ThrCreateV86Thread(fp_code, fp_stackend, 15, ShV86Handler, params); if (doWait) ThrWaitHandle(thr); /* xxx - need to clean up HndClose() implementation before we can use this */ /*HndClose(thr);*/ }