Re: [edk2] Using CSM module to run 16-bit code

Subject: Re: [edk2] Using CSM module to run 16-bit code

From: =?UTF-8?B?0JDQvdGC0L7QvSDQmtC+0YfQutC+0LI=?= <anton.kochkov@gmail.com>

To: "Li, Elvin" <elvin.li@intel.com>

Date: 2012-06-04 20:46:44

Good! With int 10h it works ok.
But what with keyboard interrupt?
After message shown i call this code (16 bit):


mov ah, 10h
int 16h
retf

But it can't read keyboard event, so can't exit from 16bit code after
key pressed and "retf".
Are there something special to initialize keyboard before call CSM?

Best regards,
Anton Kochkov.


On Mon, Jun 4, 2012 at 11:20 AM, Li, Elvin  wrote:
> Hi,
>    INT 21H is DOS function. To print message, you need use INT10 BIOS interrupt.
>
> Elvin
> -----Original Message-----
> From: xvilka@gmail.com [mailto:xvilka@gmail.com] On Behalf Of  
> Sent: Monday, June 04, 2012 2:56 PM
> To: Li, Elvin
> Cc: edk2-devel@lists.sourceforge.net
> Subject: Re: [edk2] Using CSM module to run 16-bit code
>
> Good day!
> I have forced InitAddress to be 0xE0000:
>
> InitAddress = 0xE0000;
> ...
> CopyMem((void*)InitAddress, Image, ImageSize); ...
> ImageSeg = EFI_SEGMENT(InitAddress);
> ImageOff = EFI_OFFSET(InitAddress);
> ....
> Status = Csm->FarCall86(Csm, ImageSeg, ImageOff, &Regs, NULL, 0);
>
> And then code successfully run into 16bit code, and successfully return to UEFI.
> But it doesnt show any msg (16 bit program was just simple hello world with int 21h and retf in the end.
> I suspect that something wrong with "InitAddress = 0xE0000" line, and I need choose valid long-mode address, instead of 16-bit.
> But which address i need to choose, to copy mem in 0xE0000 legacy region?
>
> Best regards,
> Anton Kochkov.
>
>
> On Fri, Jun 1, 2012 at 7:22 PM, Li, Elvin  wrote:
>> The first Unlock for 0xE0000 ~ 0xFFFFF is correct, the second Unlock is not right, Unlock is only used for 0xC0000~0xFFFFF. I am not sure the reason of you problem. In your case, you allocate the memory below 0xA0000 and copy code into there, you do not need unlock anything. Try to remove unlock and see what happens.
>>
>> Thanks
>> Elvin
>>
>>
>> -----Original Message-----
>> From: xvilka@gmail.com [mailto:xvilka@gmail.com] On Behalf Of 
>> 
>> Sent: Friday, June 01, 2012 5:49 PM
>> To: Li, Elvin
>> Cc: edk2-devel@lists.sourceforge.net
>> Subject: Re: [edk2] Using CSM module to run 16-bit code
>>
>> Fixed this, but stuck on these lines (cpu stall):
>>
>> // Unlock legacy regions to allocate code image here
>> LegReg->UnLock(LegReg, 0xE0000, 0x20000, &Granularity); // 0xE0000 -
>> LegReg->0xFFFFF UnLock(LegReg, (UINT32)RuntimeAddress, (UINT32)
>> LegReg->ImageSize,
>> &Granularity); // Place at 0xA0000
>>
>> Whats wrong here?
>>
>> Best regards,
>> Anton Kochkov.
>>
>>
>> On Fri, Jun 1, 2012 at 12:53 PM, Li, Elvin  wrote:
>>> Seems
>>>
>>>    // Calculate address for far call 0xXXXX:0xXXXX
>>>    ImageSeg = (UINT16)InitAddress;
>>>    ImageOff = (UINT16)(RuntimeAddress - InitAddress);
>>>
>>> Should be
>>>
>>>    ImageSeg = EFI_SEGMENT (InitAddress)
>>>    ImageOff = EFI_OFFSET (InitAddress)
>>>
>>>
>>> Thanks
>>> Elvin
>>>
>>>
>>> -----Original Message-----
>>> From: xvilka@gmail.com [mailto:xvilka@gmail.com] On Behalf Of 
>>> 
>>> Sent: Friday, June 01, 2012 4:45 PM
>>> To: Li, Elvin
>>> Cc: edk2-devel@lists.sourceforge.net
>>> Subject: Re: [edk2] Using CSM module to run 16-bit code
>>>
>>> Yes, stupid error. Fixed. But still doesn't work, though.
>>>
>>> Best regards,
>>> Anton Kochkov.
>>>
>>>
>>> On Fri, Jun 1, 2012 at 12:27 PM, Li, Elvin  wrote:
>>>> From your code, should the code be "Image = (void*)doscode;"?
>>>>
>>>>
>>>> -----Original Message-----
>>>> From:   [mailto:anton.kochkov@gmail.com]
>>>> Sent: Friday, June 01, 2012 3:21 PM
>>>> To: edk2-devel@lists.sourceforge.net
>>>> Subject: [edk2] Using CSM module to run 16-bit code
>>>>
>>>> Good day!
>>>> Rewriting some Option ROM to be UEFI PXE driver.
>>>> But to save time, I need start some parts of 16-bit code.
>>>> Instead of using combined UEFI+Legacy option rom images, I've decided to init device as UEFI PXE driver, but using pieces of 16-bit Legacy code.
>>>> So, I can use CSM for that.
>>>> Here is test program to understand how it works.
>>>>
>>>>
>>>>    EFI_STATUS
>>>> Status;
>>>>    EFI_LEGACY_BIOS_PROTOCOL        *Csm; // LegacyBios
>>>> protocol
>>>>    EFI_LEGACY_REGION_PROTOCOL       *LegReg; //
>>>> LegacyRegion protocol
>>>>    EFI_IA32_REGISTER_SET          Regs; // Structure to
>>>> prepare 32bit regs for 16-bit mode
>>>>    UINT32
>>>> Granularity;
>>>>    UINTN
>>>> InitAddress;      // Start of 16-bit image
>>>>    UINTN
>>>> RuntimeAddress;     // Address of entry point of 16-bit image,
>>>> can differ from Image start, e.g for MZ EXE image
>>>>    EFI_PHYSICAL_ADDRESS          PhysicalAddress; //
>>>> Physical address
>>>>    UINT16
>>>> ImageSeg = 0;
>>>>    UINT16
>>>> ImageOff = 0;
>>>>    VOID
>>>> *Image = NULL;     // Pointer to the Image
>>>>    UINT16
>>>> ImageSize = 1;
>>>>
>>>>
>>>>    // Container of 16bit code
>>>>    UINT8 doscode[23] = { 0xB4, 0x09, 0xBA, 0x08, 0x00, 0xCD,
>>>> 0x21, 0xC3, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72,
>>>> 0x6C, 0x64, 0x21, 0x0D, 0x0A, 0x24 };
>>>>
>>>>
>>>>    gST->ConOut->OutputString(gST->ConOut, (CHAR16*)HOOK_STR);
>>>>    /*
>>>> --------------------------------------------------------------------
>>>> -
>>>> -
>>>> --------
>>>> */
>>>>
>>>>    // Load 16-bit code image
>>>>    Image = (void*)&doscode;
>>>>    ImageSize = sizeof(doscode);
>>>>
>>>>    /*
>>>> --------------------------------------------------------------------
>>>> -
>>>> -
>>>> --------
>>>> */
>>>>
>>>>    // Locate CSM LegacyBios protocol
>>>>    Status = gBS->LocateProtocol(&gEfiLegacyBiosProtocolGuid,
>>>> NULL, (void **)&Csm);
>>>>
>>>>    // Locate CSM LegacyRegion protocol
>>>>    Status = gBS->LocateProtocol(&gEfiLegacyRegionProtocolGuid,
>>>> NULL, (void **)&LegReg);
>>>>
>>>>    // Prepare registers to jump into 16-bit mode
>>>>    ZeroMem(&Regs, sizeof (EFI_IA32_REGISTER_SET));
>>>>
>>>>    PhysicalAddress = CONVENTIONAL_MEMORY_TOP; // 0xA0000
>>>>
>>>>    // Allocate memmory for Image
>>>>    Status = gBS->AllocatePages(AllocateMaxAddress,
>>>> EfiBootServicesCode, EFI_SIZE_TO_PAGES (ImageSize),
>>>> &PhysicalAddress);
>>>>
>>>>    // Set entry point address
>>>>    InitAddress = (UINTN) PhysicalAddress;
>>>>    RuntimeAddress = InitAddress; // Same, because we are using
>>>> not PCI ROM image, but plain code
>>>>
>>>>    // Unlock legacy regions to allocate code image here
>>>>    LegReg->UnLock(LegReg, 0xE0000, 0x20000, &Granularity); //
>>>> 0xE0000 - 0xFFFFF
>>>>    LegReg->UnLock(LegReg, (UINT32)RuntimeAddress, (UINT32)
>>>> ImageSize, &Granularity); // Place at 0xA0000
>>>>
>>>>    // Copy code image into legacy region
>>>>    CopyMem((void *)InitAddress, Image, ImageSize);
>>>>
>>>>    // Calculate address for far call 0xXXXX:0xXXXX
>>>>    ImageSeg = (UINT16)InitAddress;
>>>>    ImageOff = (UINT16)(RuntimeAddress - InitAddress);
>>>>
>>>>    // Do far call into our code
>>>>    Status = Csm->FarCall86(Csm, ImageSeg, ImageOff, &Regs, NULL,
>>>> 0);
>>>>
>>>>    if (PhysicalAddress != 0) {
>>>>        gBS->FreePages(PhysicalAddress, EFI_SIZE_TO_PAGES
>>>> (ImageSize));
>>>>    }
>>>>
>>>>    // Lock used legacy region
>>>>    LegReg->Lock(LegReg, 0xC0000, 0x40000, &Granularity);
>>>>
>>>>
>>>>    gST->ConOut->OutputString(gST->ConOut, (CHAR16*)HOOKEND_STR);
>>>>    while (1) {
>>>>    }
>>>>    return;
>>>>
>>>> It need to show message with DOS int (16 bit code).
>>>> But looks like something wrong. I've skipped tables, interrupts, because UEFI CSM module already done that, is that right?
>>>>
>>>> Best regards,
>>>> Anton Kochkov.
>>>>
>>>> --------------------------------------------------------------------
>>>> -
>>>> -
>>>> --------
>>>> Live Security Virtual Conference
>>>> Exclusive live event will cover all the ways today's security and
>>>> threat landscape has changed and how IT managers can respond.
>>>> Discussions will include endpoint security, mobile security and the
>>>> latest in malware threats.
>>>> http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
>>>> _______________________________________________
>>>> edk2-devel mailing list
>>>> edk2-devel@lists.sourceforge.net
>>>> https://lists.sourceforge.net/lists/listinfo/edk2-devel

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/edk2-devel