int GetSmileyType(void) { int type = 0; do { printf("What type of smiley do you want to add?\n"); printf("1: INDIFFERENT\n"); printf("2: SHINY_HAPPY\n"); printf("3: GRUMPY\n"); printf("4: DOCTOR\n"); printf("5: LEADER\n"); printf("6: GERM\n"); printf("0: Cancel\n"); type = GetIntegerInput(); type--; // If input was 0 (thus decremented to -1), exit the loop if (type == -1) { break; } // Else if invalid, loop again else if ((type < INDIFFERENT) || (GERM < type)) { printf("That was not a valid choice.\n\n"); } } while ((type < INDIFFERENT) || (GERM < type)); return type; }
int GetSmileyHealth(void) { int health = 0; do { printf("What health do you want to add for the new Smiley?\n"); printf("1: HEALTHY\n"); printf("2: SICK\n"); printf("0: Cancel\n"); health = GetIntegerInput(); health--; // If input was 0 (thus decremented to -1), exit the loop if (health == -1) { break; } // Else if invalid input, print error message before next iteration else if ((health != HEALTHY) && (health != SICK)) { printf("That was not a valid choice.\n\n"); } } while ((health != HEALTHY) && (health != SICK)); return health; }
void GetCoordinate(int* coordinate, char axis) { int coordinateValue = -1; do { printf("Type in the %c coordinate to place the smiley at (0-%d):\n", axis, BOARD_SIZE - 1); coordinateValue = GetIntegerInput(); // If invalid input, print error message before next iteration if ((coordinateValue < 0) || (BOARD_SIZE <= coordinateValue)) { printf("Error: %c coordinate must be an integer between 0 and %d\n", axis, BOARD_SIZE - 1); } } while ((coordinateValue < 0) || (BOARD_SIZE <= coordinateValue)); // Now that the input is validated, assign to provided pointer *coordinate = coordinateValue; }
int main(void) { int input = -1; int smileyTypeInput = -1; int smileyHealthInput = -1; int xCoordinateInput = -1; int yCoordinateInput = -1; int turns = 0; Smiley* board[BOARD_SIZE][BOARD_SIZE]; // Set up board InitializeSmileyBoard(board); srand(time(0)); PrintSmileyDemo(); // Main loop; continues until user exits program while (input != 0) { PrintBoard(board); // Prompt for initial user input printf("Please choose from the following:\n"); printf("1: Add a smiley to the board\n"); printf("2: Do nothing this turn\n"); printf("0: Exit\n"); input = GetIntegerInput(); switch (input) { // Add smiley to the board case 1: // Smiley type smileyTypeInput = GetSmileyType(); // If type is -1, then cancel operation if (smileyTypeInput == -1) { break; } // Smiley health smileyHealthInput = GetSmileyHealth(); // If health is -1, then cancel operation if (smileyHealthInput == -1) { break; } // Coordinates GetCoordinate(&xCoordinateInput, 'x'); GetCoordinate(&yCoordinateInput, 'y'); // If the given coordinates are empty and valid, then place the Smiley there if ((IsValidCoordinatePair(xCoordinateInput, yCoordinateInput)) && (board[yCoordinateInput][xCoordinateInput] == NULL)) { board[yCoordinateInput][xCoordinateInput] = CreateSmiley((SmileyType) smileyTypeInput, (SmileyHealth) smileyHealthInput); } // Else display error message and cancel operation else { printf("There is already a Smiley at (%d, %d).\n", xCoordinateInput, yCoordinateInput); } break; case 2: printf("You wait.\n"); break; case 0: continue; default: break; } IterateBoard(board); turns++; } FreeSmileysOnBoard(board); printf("Press ENTER to continue"); getchar(); exit(0); }
EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) { InitializeLib(ImageHandle, SystemTable); CallConstructors(); EFI_STATUS Status; const char16_t* searchdir = u"\\EFI\\ChaiOS\\"; if (Status = SetWorkingDirectory(searchdir)) printf(u"Error setting working directory: %s\r\n", getError(Status)); CHAIOS_BOOT_FILES* bootfile = nullptr; while (bootfile = iterateBootFiles(bootfile)) { if (bootfile->loadLocation != nullptr) continue; //Support in-memory images EFI_FILE* file = OpenFile(bootfile->fileName, "r"); if (!file) { printf(u"Error: could not open %s: %s\r\n", bootfile->fileName, getError(getErrno())); } UINT64 fileSize = GetFileSize(file); VOID* bootfilebuf = kmalloc(fileSize+1); UINTN read = ReadFile(bootfilebuf, 1, fileSize, file); if (read < fileSize) printf(u"Read %d bytes, failed\r\n", read); else printf(u"Successfully read %d bytes\r\n", read); //Boot file is now loaded into memory CloseFile(file); bootfile->loadLocation = bootfilebuf; bootfile->fileSize = fileSize; if (bootfile->bootType == CHAIOS_BOOT_CONFIGURATION) { //We need to parse this now. INI format ((char*)bootfilebuf)[fileSize] = '\0'; ini_parse_string((const char*)bootfilebuf, &bootini_handler, nullptr); } } //size_t value = GetIntegerInput(u"Enter scrolling lines configuration: "); //set_scrolllines(value); UINT32 AutoMode = IterateGraphicsMode(&match_config_resoultion); EFI_GRAPHICS_OUTPUT_MODE_INFORMATION* info; UINTN SizeOfInfo; if (AutoMode == UINT32_MAX) { if (!EFI_ERROR(GetGraphicsModeInfo(AutoMode, &info, &SizeOfInfo))) { IterateGraphicsMode(&print_graphics_mode); size_t value = GetIntegerInput(u"Enter boot graphics mode: "); SetGraphicsMode(value); AutoMode = value; } } else { SetGraphicsMode(AutoMode); } if (!EFI_ERROR(GetGraphicsModeInfo(AutoMode, &info, &SizeOfInfo))) { printf(u"Graphics mode %d: %dx%d\r\n", AutoMode, info->HorizontalResolution, info->VerticalResolution); } puts(u"ChaiOS 0.09 UEFI Loader\r\n"); int majorver = SystemTable->Hdr.Revision / (1 << 16); int minorver = SystemTable->Hdr.Revision % (1 << 16); printf(u"Firmware Vendor: %s, version: %d (UEFI:%d.%d)\r\n", SystemTable->FirmwareVendor, SystemTable->FirmwareRevision, majorver, minorver); //Read ACPI configuration tables //startup_acpi(SystemTable); //startup_multiprocessor(); const size_t EARLY_PAGE_STACK_SIZE = 1024*1024; EFI_PHYSICAL_ADDRESS earlyPhypageStack = 0; if (EFI_ERROR(SystemTable->BootServices->AllocatePages(AllocateAnyPages, EfiLoaderData, EARLY_PAGE_STACK_SIZE / EFI_PAGE_SIZE, &earlyPhypageStack))) { puts(u"Could not allocate page stack\r\n"); return EFI_OUT_OF_RESOURCES; } SystemTable->BootServices->SetWatchdogTimer(0, 0, 0, nullptr); PrepareExitBootServices(); EfiMemoryMap map; map.MemMapSize = map.MapKey = map.DescriptorSize = map.DescriptorVersion = 0; SystemTable->BootServices->GetMemoryMap(&map.MemMapSize, nullptr, &map.MapKey, &map.DescriptorSize, &map.DescriptorVersion); //Give a nice bit of room to spare (memory map can change) map.MemMapSize += 16 * map.DescriptorSize; map.memmap = (EFI_MEMORY_DESCRIPTOR*)kmalloc(map.MemMapSize); //Allocate a nice buffer SystemTable->BootServices->GetMemoryMap(&map.MemMapSize, map.memmap, &map.MapKey, &map.DescriptorSize, &map.DescriptorVersion); printf(u"EFI Memory Map: Descriptor size %d\n", map.DescriptorSize); #if 0 //Dump the UEFI memory map to a file for testing EFI_FILE* file = OpenFile(u"efimap.dat", "w"); if (!file) { printf(u"Error: could not open %s: %s\r\n", u"efimap.dat", getError(getErrno())); } WriteFile(map.memmap, 1, map.MemMapSize, file); CloseFile(file); #endif if (EFI_ERROR(Status = SystemTable->BootServices->ExitBootServices(ImageHandle, map.MapKey))) { printf(u"Failed to exit boot services: %s\r\n", getError(Status)); UINTN index; SystemTable->BootServices->WaitForEvent(1, &SystemTable->ConIn->WaitForKey, &index); return EFI_SUCCESS; } //We need to take control of the hardware now. Setup basic memory management setLiballocAllocator(nullptr, nullptr); InitializePmmngr(map, (void*)earlyPhypageStack, EARLY_PAGE_STACK_SIZE); puts(u"Physical memory manager intialized\n"); arch_initialize_paging(); puts(u"Paging initialized\n"); setLiballocAllocator(&arch_allocate_pages, &arch_free_pages); //Now load the OS! bootfile = nullptr; kimage_entry kentry = nullptr; KLOAD_HANDLE kernel = NULL; while (bootfile = iterateBootFiles(bootfile)) { printf(u"Boot file: %s @ %x, length %d, type %d\n", bootfile->fileName, bootfile->loadLocation, bootfile->fileSize, bootfile->bootType); if (!bootfile->loadLocation) continue; if (bootfile->bootType == CHAIOS_DLL) { KLOAD_HANDLE dll = LoadImage(bootfile->loadLocation, bootfile->fileName); if (GetProcAddress(dll, "memcpy")) { set_memcpy((memcpy_proc)GetProcAddress(dll, "memcpy")); } } else if (bootfile->bootType == CHAIOS_KERNEL) { kernel = LoadImage(bootfile->loadLocation, bootfile->fileName); kentry = GetEntryPoint(kernel); } } size_t kstacksize = GetStackSize(kernel); if (!paging_map(stackaddr, PADDR_T_MAX, kstacksize, PAGE_ATTRIBUTE_WRITABLE)) { puts(u"Error: could not allocate kernel stack\n"); while (1); } KERNEL_BOOT_INFO bootinfo; fill_pmmngr_info(bootinfo.pmmngr_info); fill_arch_paging_info(bootinfo.paging_info); fill_modloader_info(bootinfo.modloader_info); get_framebuffer_info(bootinfo.fbinfo); populate_kterm_info(bootinfo.kterm_status); bootinfo.efi_system_table = SystemTable; bootinfo.memory_map = ↦ bootinfo.loaded_files = &bootfiles; bootinfo.boottype = CHAIOS_BOOT_TYPE_UEFI; bootinfo.printf_proc = &printf; bootinfo.puts_proc = &puts; printf(u"Success: Kernel entry point at %x, stack at %x, length %x\n", kentry, stackaddr, kstacksize); call_kernel(&bootinfo, kentry, stackaddr, kstacksize); puts(u"Kernel returned"); while (1); }