- LOG("NumPages: %i", pBinary->NumPages);
-
- // Read Data
- for(i = 0; i < pBinary->NumPages; i ++)
- {
- Uint dest;
- tPAddr paddr;
- paddr = (Uint)MM_AllocPhys();
- if(paddr == 0) {
- Log_Warning("BIN", "Binary_DoLoad - Physical memory allocation failed");
- for( ; i--; ) {
- MM_DerefPhys( pBinary->Pages[i].Physical );
- }
- return NULL;
- }
- MM_RefPhys( paddr ); // Make sure it is _NOT_ freed until we want it to be
- dest = MM_MapTemp( paddr );
- dest += pBinary->Pages[i].Virtual & 0xFFF;
- LOG("dest = 0x%x, paddr = 0x%x", dest, paddr);
- LOG("Pages[%i]={Physical:0x%llx,Virtual:%p,Size:0x%x}",
- i, pBinary->Pages[i].Physical, pBinary->Pages[i].Virtual, pBinary->Pages[i].Size);
-
- // Pure Empty Page
- if(pBinary->Pages[i].Physical == -1) {
- LOG("%i - ZERO", i);
- memset( (void*)dest, 0, 1024 - (pBinary->Pages[i].Virtual & 0xFFF) );
- }
- else
- {
- VFS_Seek( fp, pBinary->Pages[i].Physical, 1 );
- // If the address is not aligned, or the page is not full
- // sized, copy part of it
- if( (dest & 0xFFF) > 0 || pBinary->Pages[i].Size < 0x1000)
- {
- // Validate the size to prevent Kernel page faults
- // Clips to one page and prints a warning
- if( pBinary->Pages[i].Size + (dest & 0xFFF) > 0x1000) {
- Log_Warning("Binary", "Loader error: Page %i (%p) of '%s' is %i bytes (> 4096)",
- i, pBinary->Pages[i].Virtual, truePath,
- (dest&0xFFF) + pBinary->Pages[i].Size);
- pBinary->Pages[i].Size = 0x1000 - (dest & 0xFFF);
- }
- LOG("%i - 0x%llx - 0x%x bytes",
- i, pBinary->Pages[i].Physical, pBinary->Pages[i].Size);
- // Zero from `dest` to the end of the page
- memset( (void*)dest, 0, 0x1000 - (dest&0xFFF) );
- // Read in the data
- VFS_Read( fp, pBinary->Pages[i].Size, (void*)dest );
- }
- // Full page
- else
- {
- // Check if the page is oversized
- if(pBinary->Pages[i].Size > 0x1000)
- Log_Warning("Binary", "Loader error - Page %i (%p) of '%s' is %i bytes (> 4096)",
- i, pBinary->Pages[i].Virtual, truePath,
- pBinary->Pages[i].Size);
- // Read data
- LOG("%i - 0x%x", i, pBinary->Pages[i].Physical);
- VFS_Read( fp, 0x1000, (void*)dest );
- }
- }
- pBinary->Pages[i].Physical = paddr;
- MM_FreeTemp( dest );
- }
- LOG("Page Count: %i", pBinary->NumPages);
-
- // Close File
- VFS_Close(fp);