|
| 1 | +/* |
| 2 | + * PROJECT: FreeLoader |
| 3 | + * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later) |
| 4 | + * PURPOSE: Real-mode FreeLoader relocator |
| 5 | + * COPYRIGHT: Copyright 2024-2025 Daniel Victor <[email protected]> |
| 6 | + */ |
| 7 | + |
| 8 | +#define RELOCATOR_BLOCK_SIZE HEX(200) |
| 9 | +#define RELOCATOR_BLOCK_SIZE_SEGMENT (RELOCATOR_BLOCK_SIZE / 16) |
| 10 | + |
| 11 | +#define RELOCATOR_FUNCTION_BASE FREELDR_BASE |
| 12 | + |
| 13 | +#define RELOCATOR_PROGRAM_SIZE (MEMORY_MARGIN - FREELDR_BASE) |
| 14 | +#define RELOCATOR_PROGRAM_SIZE_BLOCKS ((RELOCATOR_PROGRAM_SIZE + (RELOCATOR_BLOCK_SIZE - 1)) / RELOCATOR_BLOCK_SIZE) |
| 15 | +#define RELOCATOR_PROGRAM_SIZE_ALIGNED (RELOCATOR_PROGRAM_SIZE_BLOCKS * RELOCATOR_BLOCK_SIZE) |
| 16 | + |
| 17 | +/* WARNING: This function must be called at the real-mode entry point since it restarts FreeLoader |
| 18 | + * This function will update BSS_CurrentSegmentBaseAddress and relocate if necessary */ |
| 19 | +RelocateFreeLdr: |
| 20 | + /* Clear direction flag */ |
| 21 | + cld |
| 22 | + |
| 23 | + /* Call the sub-function to retrieve the base address */ |
| 24 | + call .SubCall |
| 25 | +.SubCall: |
| 26 | + /* Pop the return address into BX. Since the return address is consumed, |
| 27 | + * this function does not return to RelocateFreeLdr */ |
| 28 | + pop bx |
| 29 | + |
| 30 | + /* Calculate IP base address and convert to segment value */ |
| 31 | + sub bx, offset .SubCall - RELOCATOR_FUNCTION_BASE |
| 32 | + shr bx, 4 |
| 33 | + |
| 34 | + /* Load CS base address into AX and add it with IP base address */ |
| 35 | + mov ax, cs |
| 36 | + add ax, bx |
| 37 | + |
| 38 | + /* Store the calculated base address (low 16 bits) */ |
| 39 | + mov word ptr fs:[BSS_CurrentSegmentBaseAddress], ax |
| 40 | + |
| 41 | +.RelocateFrLdr16: |
| 42 | + /* If the current base address is the same as FREELDR_BASE then it's not necessary to relocate */ |
| 43 | + cmp ax, FREELDR_BASE / 16 |
| 44 | + jz .NotNecessaryRelocation |
| 45 | + |
| 46 | + /* Setup source segment */ |
| 47 | + mov ds, ax |
| 48 | + |
| 49 | + /* Setup destination segment */ |
| 50 | + xor ax, ax |
| 51 | + mov es, ax |
| 52 | + |
| 53 | + /* Copy the realmode part of FRLDR16 code to TEMPCODE16_BASE */ |
| 54 | + mov di, TEMPCODE16_BASE |
| 55 | + mov si, offset RELOCATOR_FUNCTION_START - RELOCATOR_FUNCTION_BASE |
| 56 | + mov cx, offset RELOCATOR_FUNCTION_END - RELOCATOR_FUNCTION_START |
| 57 | + rep movsb |
| 58 | + |
| 59 | + /* Jump to relocated realmode part */ |
| 60 | + ljmp16 TEMPCODE16_BASE / 16, 0 |
| 61 | + |
| 62 | +RELOCATOR_FUNCTION_START: |
| 63 | + |
| 64 | +.SetupRelocation: |
| 65 | + /* Save DX (contains the boot drive and partition numbers) */ |
| 66 | + push dx |
| 67 | + |
| 68 | + /* Prepare the copy size in 16-byte blocks */ |
| 69 | + mov dx, RELOCATOR_PROGRAM_SIZE_ALIGNED / 16 |
| 70 | + |
| 71 | + /* Prepare the destination register */ |
| 72 | + mov ax, FREELDR_BASE / 16 |
| 73 | + mov es, ax |
| 74 | + |
| 75 | + /* Prepare the source register */ |
| 76 | + mov ax, word ptr fs:[BSS_CurrentSegmentBaseAddress] |
| 77 | + mov ds, ax |
| 78 | + |
| 79 | + /* If current base address is lower than the final one then just copy it backwards */ |
| 80 | + cmp ax, FREELDR_BASE / 16 |
| 81 | + jb .SetupRelocateLoopBackward |
| 82 | +.RelocateLoopForward: |
| 83 | + /* Abort the loop if DX is below `RELOCATOR_BLOCK_SIZE_SEGMENT` */ |
| 84 | + cmp dx, RELOCATOR_BLOCK_SIZE_SEGMENT |
| 85 | + jb .FinishedRelocation |
| 86 | + |
| 87 | + /* Copy `RELOCATOR_BLOCK_SIZE` bytes from source to destination */ |
| 88 | + xor di, di |
| 89 | + xor si, si |
| 90 | + mov cx, RELOCATOR_BLOCK_SIZE |
| 91 | + rep movsb |
| 92 | + |
| 93 | + /* Increment ES */ |
| 94 | + mov ax, es |
| 95 | + add ax, RELOCATOR_BLOCK_SIZE_SEGMENT |
| 96 | + mov es, ax |
| 97 | + |
| 98 | + /* Increment DS */ |
| 99 | + mov ax, ds |
| 100 | + add ax, RELOCATOR_BLOCK_SIZE_SEGMENT |
| 101 | + mov ds, ax |
| 102 | + |
| 103 | + /* Repeat the loop */ |
| 104 | + sub dx, RELOCATOR_BLOCK_SIZE_SEGMENT |
| 105 | + jmp .RelocateLoopForward |
| 106 | + |
| 107 | +.SetupRelocateLoopBackward: |
| 108 | + /* Set direction flag for inverted copy */ |
| 109 | + std |
| 110 | + |
| 111 | + /* Move destination register to the end */ |
| 112 | + mov ax, es |
| 113 | + add ax, dx |
| 114 | + mov es, ax |
| 115 | + |
| 116 | + /* Move source register to the end */ |
| 117 | + mov ax, ds |
| 118 | + add ax, dx |
| 119 | + mov ds, ax |
| 120 | +.RelocateLoopBackward: |
| 121 | + /* Abort the loop if DX is below `RELOCATOR_BLOCK_SIZE_SEGMENT` */ |
| 122 | + cmp dx, RELOCATOR_BLOCK_SIZE_SEGMENT |
| 123 | + jb .FinishedRelocation |
| 124 | + |
| 125 | + /* Decrement ES */ |
| 126 | + mov ax, es |
| 127 | + sub ax, RELOCATOR_BLOCK_SIZE_SEGMENT |
| 128 | + mov es, ax |
| 129 | + |
| 130 | + /* Decrement DS */ |
| 131 | + mov ax, ds |
| 132 | + sub ax, RELOCATOR_BLOCK_SIZE_SEGMENT |
| 133 | + mov ds, ax |
| 134 | + |
| 135 | + /* Copy `RELOCATOR_BLOCK_SIZE` bytes from source to destination */ |
| 136 | + mov di, RELOCATOR_BLOCK_SIZE - 1 |
| 137 | + mov si, di |
| 138 | + mov cx, RELOCATOR_BLOCK_SIZE |
| 139 | + rep movsb |
| 140 | + |
| 141 | + /* Repeat the loop */ |
| 142 | + sub dx, RELOCATOR_BLOCK_SIZE_SEGMENT |
| 143 | + jmp .RelocateLoopBackward |
| 144 | + |
| 145 | +.FinishedRelocation: |
| 146 | + /* Restore DX and restart FreeLoader with new base address */ |
| 147 | + pop dx |
| 148 | + ljmp16 FREELDR_BASE / 16, 0 |
| 149 | + |
| 150 | +RELOCATOR_FUNCTION_END: |
| 151 | + |
| 152 | +.NotNecessaryRelocation: |
| 153 | + ret |
0 commit comments