%ifndef __X86LIB_RMOROM__
%define __X86LIB_RMOROM__

    %include "orom.asm"
    %include "rmaddr.asm"

    ;===========================================================================
    ; Description: Locates the next Option ROM loaded into memory.
    ;---------------------------------------------------------------------------
    ; Parameters:-
    ;     EBX - The physical address to start searching from. Use a value of 0
    ;           to search from the beginning of the Option ROM area.
    ; Returns:-
    ;     EBX   - The physical address to resume the search from; this will be 0
    ;             if the search has reached the end of the Option ROM area.
    ;     DS:SI - The real-mode segment:offset of the Option ROM if one was
    ;             found; this will be 0000:0000 if none could be found from the
    ;             specified physical address to the end of the Option ROM area.
    ;===========================================================================
    rmLocateNextOptionROM:
            push ax
            or ebx, ebx
            jnz .validateNextOptionROM
            mov ebx, OPTION_ROM_ADDRESS_RANGE_MINIMUM
        .validateNextOptionROM:
            cmp ebx, OPTION_ROM_ADDRESS_RANGE_MAXIMUM
            jge .noneFound
            call rmConvertAddressToSegmentOffset
            add ebx, OPTION_ROM_BOUNDRY_INTERVAL
            mov ax, [ds:si]
            xor ax, OPTION_ROM_SIGNATURE
            jz .done
            jmp .validateNextOptionROM
        .noneFound:
            mov ebx, 0
            mov ds, bx
            mov si, bx
        .done:
            pop ax
            ret
    
%endif
