2021年11月23日 星期二

[X86] Real Mode to Protected Mode


參考來源:
Intel 64 and IA-32 Architectures Software Developer Manuals
AMD64 Architecture Programmer's Manual

System Reset後,BIOS在 SEC階段一開始就會從 Real Mode切換到 Protected Mode。
切換 Protected Mode前,BIOS最少要設定 GDTR及 Control Registers。

GDTR
透過 lgdt命令將 ROM中寫好的 table載入 GDT中,

Control Register
Enable CR0的 PE flag


2021年11月8日 星期一

[WHLK] Directed FX System Verification Test

參考網站
introduction-to-the-directed-power-management-framework
pwrtest-directedfx-scenario

此測項可用 pwrtest.exe來替代做簡單的測試: pwrtest.exe /directedfx /device:path path可從 device manager中的 device instance path查詢

2021年10月5日 星期二

[X86] Reset Vector


參考來源:
Intel 64 and IA-32 Architectures Software Developer Manuals
AMD64 Architecture Programmer's Manual

當前 x86架構下,CPU第一個指令執行位置是從 0xFFFF_FFF0開始。但 CPU啟動時是 Real

Mode,照理說只能定址到 1MB位置。所以其是透過 segment register(CS)中 invisible register

的機制來達成。

一般情況下 CS的 Base值會是 Selector左移 4 bits的值,但 CPU初始時會將 CS的 Selector及

Base設成 0xF000和 0xFFFF_0000。由於 EIP初始的值為 0xFFF0,如此將初始位置指向

0xFFFF_FFF0 (0xFFFF_0000 + 0xFFF0)。


0xFFFF_FFF0 是 ROM映射的位置,所以 BIOS會將 Reset Vector程式碼放在與之相應的位置。

以16 MB(0x100_0000)的BIOS來說,其會映射在0xFF00_0000 - 0xFFFF_FFFF


用 RWEverything實際看機器上 0xFFFF_FFF0位置的值

其 machine code為0x90 0x90 0xE9,後面則是 16 bits jmp address(0xC39B)。

可與EDK2中 Reset Vector程式碼對照 UefiCpuPkg\SecCore\Ia32\ResetVec.nasmb

;
; For IA32, the reset vector must be at 0xFFFFFFF0, i.e., 4G-16 byte
; Execution starts here upon power-on/platform-reset.
;
ResetHandler:
  nop
  nop
ApStartup:
  ;
  ; Jmp Rel16 instruction
  ; Use machine code directly in case of the assembler optimization
  ; SEC entry point relative address will be fixed up by some build tool.
  ;
  ; Typically, SEC entry point is the function _ModuleEntryPoint() defined in
  ; SecEntry.asm
  ;
  DB 0e9h
  DW -3

在兩個 nop後,接 near jump 0xE9,來確保 CS selector的值不會被改變。後面預留 DW

會由 build tool來填入 SEC進入點的位址。

上圖RW看到的位址是 0xC39B,此值與當前的 IP值 0xFFF5 (0xFFFF_FFF5)相加,就能找到

SEC的進入點在 0xFFFF_C390的位置。

開始的指令為0xDB 0xE3,是 FNINIT的 machine code。

2021年9月16日 星期四

[X86] Memory Paging


參考來源:
Intel 64 and IA-32 Architectures Software Developer Manuals
AMD64 Architecture Programmer's Manual
The Intel Microprocessors by Barry B.Brey

Paging Registers


Paging行為主要由 control register所控制。


若CR0的 PG(Paging)=0,程式所使用的 linear address就是 physical address。

PG=1,則 linear address會透過 Paging的機制轉換成 physical address。


CR3的PCD(Page-level Cache Disable)與 PWT(Page-level Write-Through) bit控制Page

Directory 的 cache type。

PCD=0為cachable,PCD=1為not cachable。PWT=0 cache type為 writeback,PCD=1 為

writethrough。

Page-Directory Base指向 Page Directory 的 physical address。


linear addresspage directory entrypage table entrymemory page offset

address組成。page directory entry(10 bits)為 1024(2^10) 個 page directory的 index。

每個 page directory entry可表示 4M的記憶體(1024個page table * 4k)。


而page table entry(10 bits)為 1024(2^10)個 page table的 index。page table會指向某段4K 記憶體。

memory page offset address(12 bits) 為該段 4K記憶體的offset。


2021年9月10日 星期五

Windows Server 延長使用授權 - slmgr

參考網站
https://docs.microsoft.com/en-us/windows-server/get-started/activation-slmgr-vbs-options

Windows Server 評估版在過了使用期限後,還是可以透過 slmgr來延長。


透過 Win + R快捷鍵執行:

查看使用期限及剩餘可延長次數

slmgr.vbs /dlv

延長使用授權

slmgr.vbs /rearm

2021年5月17日 星期一

Beyond BIOS Note - CH3 UEFI Driver Model


Things should be made as simple as possible - but not simpler
- Albert Einstein

Driver initialization

Load Image

Driver image的檔案必須儲存於ROM、硬碟或網路等媒體裝置,當系統找到driver image時,
就可以透過 gBS->LoadImage() 將image加載至記憶體,而此Image必須符合PE/COFF 格式。

當gBS->LoadImage()執行後,系統就會為driver建立一個handle,這個handle被稱為
Image Handle,並且將一個 EFI_LOADED_IMAGE_PROTOCOL實體放在這handle下。
這時的driver還沒有執行(start),只存在於記憶體之中。

Image Handle
   └ ─ ─ ─ EFI_LOADED_IMAGE_PROTOCOL

Start Image

UEFI Driver Model 的driver 不能直接touch hardware,只能在本身的 Image Handle上
Install Protocol,且還必須安裝EFI_DRIVER_BINDING_PROTOCOL,有
EFI_DRIVER_BINDING_PROTOCOL 的Image Handle 則被稱為Driver Image Handle

若driver要能被unload,則必須實作EFI_LOADED_IMAGE_PROTOCOL 中的Unload() function。

Driver Image Handle
   └ ─ ─ ─ EFI_LOADED_IMAGE_PROTOCOL
   └ ─ ─ ─ EFI_DRIVER_BINDING_PROTOCOL
   └ ─ ─ ─ EFI_DRIVER_CONFIGURATION_PROTOCOL (optional)
   └ ─ ─ ─ EFI_DRIVER_DIAGNOSTICS_PROTOCOL (optional)
   └ ─ ─ ─ EFI_DRIVER_COMPONENT_NAME2_PROTOCOL (optional)

Host Bus Controllers

UEFI Driver Model driver 通常用來操作一至多個controller,而在driver與controller連接之
前,需要操作某些controller,這些controller稱為Host Bus Controller。

每個host bridge都表示成一個device handle,device handle中有 Device Path Protocol及
其IO抽象化操作的Protocol。以PCI Host Bus Controller為例,其提供PCI Host Bridge IO
Protocol。

PCI Host Bridge Device Handle
   └ ─ ─ ─ EFI_DEVICE_PATH_PROTOCOL
   └ ─ ─ ─ EFI_PCI_HOST_BRIDGE_IO_PROTOCOL

PCI Bus Driver可以連接在此PCI Host Bridge,並建立其child handle給每個系統中的PCI
device。而PCI Device Driver 則必須連接這些child handle並提供其抽象化IO操作給系統使用。

Device Drivers

Device Driver不允許建立新的device handle,只在現有的device handle上添加protocol。
其最常見的行為是在Bus driver所建立的handle上提供IO抽象化操作,例如Simple Text
Output、Simple Input、Block I/O及Simple Network Protocol。

Device Handle
   └ ─ ─ ─ EFI_DEVICE_PATH_PROTOCOL
   └ ─ ─ ─ EFI_XYZ_IO_PROTOCOL

    ↓ Start()   ↑ Stop()

Device Handle
   └ ─ ─ ─ EFI_DEVICE_PATH_PROTOCOL
   └ ─ ─ ─ EFI_XYZ_IO_PROTOCOL
   └ ─ ─ ─ EFI_BLOCK_IO_PROTOCOL

連接Device handle的Device Driver必須有Driver Binding Protocol在其本身的image handle上。

Driver Binding Protocol包含Supported ()、Start ()及Stop ()三個functions。
  • Supported ()
  • 功用為測試此driver是否支援特定controller。以上面的Device handle為例,driver
    可以檢查此device handle是否支援 Device Path Protocol及EFI_XYZ_IO_PROTOCOL。
    若 Supported()通過,driver就能透過 Start()來連接controller。

  • Start ()
  • driver透過Start() 在device handle上添加額外的IO protocol。以上面例子來看
    ,Block IO protocol就被建立在device handle上。

  • Stop ()
  • 相對於Start(),Stop() 用來終止driver對device handle的操作,並要負責將原來
    driver安裝在device handle 上的任何protocol移除。

Support ()、Start ()及Stop ()需呼叫OpenProtocol ()來取得protocol和CloseProtocol ()
來釋出Protocol。OpenProtocol ()及CloseProtocol ()會更新handle database,讓系統能追蹤
哪些protocol正在被使用。可透過OpenProtocolInformation ()來獲取component正在使用
protocol的相關列表。

Bus Drivers

Bus driver 負責對其 bus上的 child controller建立 device handle。
A、B、C、D和E代表此Bus controller 的child controller。其上的箭頭代表其parent controller
,如果此Bus controller 為 Host Bus Controller的話,則沒有parent controller。

    ↙
 Bus Controller

    ↓ Start()   ↑ Stop()

    ↙
 Bus Controller
    └ ─ A
    └ ─ B
    └ ─ C
    └ ─ D
    └ ─ E

Bus driver至少必須在其child handle上安裝IO抽象化操作的protocol(EFI_XYZ_IO_PROTOCOL),
若child handle代表physical device,則還需安裝DEVICE_PATH_PROTOCOL。Bus Specific
Driver Protocol為optional,在driver connect child controller時會使用到(Boot Service 的
ConnectController ())。

Child Device Handle
   └ ─ ─ ─ EFI_DEVICE_PATH_PROTOCOL
   └ ─ ─ ─ EFI_XYZ_IO_PROTOCOL
   └ ─ ─ ─ EFI_BUS_SPECIAL_DRIVER_OVERRIDE_PROTOCOL (optional)

Platform Components

driver 的connect與disconnect controller由platform firmware透過Boot Service
ConnectController()和DisconnectController()來決定,通常為UEFI Boot Manager
的一部分。

若platform想要執行系統檢測或安裝作業系統,則其會connect driver到所有可能的 boot
device。若platform想開機到預安裝好的作業系統,則其只需connect 該作業系統需要的
device及其所需的driver。

platform 也可以選擇安裝optional的 protocol Platform Driver Override Protocol,其作用與
Bus Specific Driver Protocol相同,但擁有更高的priority。

Hot Plug Event

當 Hot Plug Event因新增 device觸發時,Bus driver需要負責:
  1. 建立 device 的child handle
  2. 呼叫 ConnectController()

2021年5月13日 星期四

[Python] 將 Qt Designer的 .ui檔轉換成 .py檔

Qt Designer

PySide2

使用 pyside2-uic

pyside2-uic.exe <UI_FILE> -o <OUTPUT_FILE>

pyside2-uic預設安裝位置為%PYTHON_INSTALL_PATH%\Scripts\



PyQt5 & PyQt6

使用 pyuic5pyuic6

pyuic6.exe <UI_FILE> -o <OUTPUT_FILE>

pyuic5及 pyuic6預設安裝位置為%PYTHON_INSTALL_PATH%\Scripts\


2021年5月5日 星期三

建置 Windows Debugger(WinDbg) 環境 via Serial Cable

本篇文章參考
Debugging Tools for Windows

其他WinDbg相關細節
建置 Windows Debugger(WinDbg) 環境 via USB 3.0 cable

Setup Target Computer

1. 使用 Device Manager確認 COM port number。

2. 使用系統管理員權限開啟command prompt,並輸入以下命令
bcdedit /debug on
bcdedit /dbgsettings serial debugport:n baudrate:115200
n為 COM port number。

3. 重新開機。

Setup and Use WinDbg

1. 開啟WinDbg
(C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\windbg.exe)

2. 點選"File" > "Kernel Debugging"

3. 標籤頁切換到"COM",並輸入 HOST端所連接的 COM port number。

3. 按下確定,開始執行WinDbg

2021年4月27日 星期二

iASL - ASL Optimizing Compiler and Disassembler

參考網站
ACPICA

Errors, Warnings, and Remarks

-ve
只回報Error,忽略Warning及Remark。

編譯 DSDT0000.dsl時只回報 Error iasl.exe -ve DSDT0000.dsl

AML Disassembler

-d <f1 f2 ...>
反組譯 AML檔案回 ASL(*.dsl)

反組譯 DSDT0000.bin回 DSDT0000.dsl 的ASL檔案 iasl.exe -d DSDT0000.bin

-e <f1 f2 ...>
在反組譯AML時,會遇到有些 unresolve external control method,用以指定含有其
external control method的 AML檔

反組譯DSDT0000.bin時,指定 SSDT0000.bin及 SSD10000.bin為含有其
external control method的AML檔
iasl.exe -e SSDT0000.bin SSD10000.bin -d DSDT0000.bin

指定所有 SSD*.bin為含有DSDT0000.bin external control method的AML檔
iasl.exe -e SSD*.bin -d DSDT0000.bin

2021年4月13日 星期二

asl.exe - Microsoft ASL Compiler

參考網站
microsoft-asl-compiler

Windows在使用 ACPI table時,不是每次都從記憶體存取,而是暫存在其登錄檔,透過登錄檔
來存取。可以透過 regedit登錄檔編輯程式來讀取修改 ACPI的登錄檔,其路徑為
Computer\HKEY_LOCAL_MACHINE\HARDWARE\ACPI

而 Microsoft的 WDK提供 asl.exe可以用來取出或取代其中的 ACPI table。

/tab=<TabSig>
將 <TabSig>的 AML反組譯回 ASL檔案,<TabSig>可從 regedit查詢。

取出 DSDT.asl
asl.exe /tab=DSDT

取出所有 ACPI table並存成 ACPI.txt
asl.exe /tab=*

/nologo
不印出logo banner的訊息。

Example 1: without /nologo
asl.exe /tab=DSDT
Output 1:
Microsoft ACPI Source Language Assembler Version 5.0.0NT
Copyright (c) 1996,2014 Microsoft Corporation
Compliant with the ACPI 5.0 Specification

Example 2: with /nologo
asl.exe /nologo /tab=DSDT
Output 2:


/c
將 table存成 .bin檔

取出 DSDT的 AML並存成 DSDT0000.bin
asl.exe /c /tab=DSDT

ACPI-table-load usage


只能用於已存在在系統中的ACPI table

/loadtable
將ACPI table load進 registry中

將 AMLFile load進 registry中
asl.exe /loadtable [-v] [-d] <AMLFile>
-v: verbose mode
-d: 移除先前所 load的 AML檔

在 Windows下修改及替換 DSDT或SSDT table

1. 以DSDT為例,先使用 asl.exe將 DSDT的 AML從 registry取出
asl.exe /tab=DSDT /c
會得到 DSDT0000.BIN。

2. 使用 ASL compiler iasl.exe將 DSDT0000.bin 反組譯。
iasl.exe -d DSDT0000.bin
若是反組譯成功,就會產生 DSDT0000.dsl的 ASL檔。
有時會遇到缺少 external control method的情況導致反組譯失敗,這時就可以使用 -e
參數來 include包含 external control method的 AML檔。

EX: Include SSD0及 SSD1
iasl.exe -e SSD00000.bin SSD10000.bin -d DSDT0000.bin
或是使用 -fe來 include描述 external control method的檔案。

3. 依照需求修改 DSDT0000.dsl

EX: 在 DSDT0000.dsl中加入 Serial port的定義

DefinitionBlock ("", "DSDT", 2, "ACRSYS", "ACRPRDCT", 0x00000000)
{
    External (_GPE.HLVT, MethodObj)    // 0 Arguments
    External (_PR_.BGIA, UnknownObj)
    External (_PR_.BGMA, UnknownObj)
    External (_PR_.BGMS, UnknownObj)
......
    OperationRegion (SERP, SystemIO, 0x3F8, 0x8)
    Field (SERP, ByteAcc, NoLock, Preserve)
    {
      THRR, 8,
      , 8,
      , 8,
      , 8,
      , 8,
      LSRR, 8,
      MSRR, 8
    }

    Name (SS1, Zero)
    Name (SS2, Zero)
......

4. 使用 iasl.exe compile修改過的DSDT0000.dsl
iasl.exe -ve DSDT0000.dsl
成功後就會產生 DSDT0000.aml。
-ve為忽略 warning及 remark,只顯示error。

5. 將DSDT0000.aml load到 registry
asl.exe /loadtable DSDT0000.aml

6. 啟動 Windows test mode
bcdedit /set testsigning on
重新開機以後就能生效。

2021年3月29日 星期一

[Python] Nuitka - 將Python 打包成執行檔


官方網址
https://nuitka.net
https://github.com/Nuitka

Windows


使用Nuitka 將hello.py 打包
python -m nuitka --mingw64 hello.py
打包時會要求下載 C Caching tool及 MinGW64 C compiler。


打包成一個獨立資料夾
python -m nuitka --mingw64 --standalone hello.py
執行完後會產生 .build及 .dist, .dist就是可以用來單獨執行的資料夾。

2021年3月20日 星期六

The Blue Nowhere Developer Software Select


程式碼編輯器 (Code Editor)


Visual Studio Code
https://code.visualstudio.com

Buckup
Sublime Text
https://www.sublimetext.com

Notepad++
https://notepad-plus-plus.org


內容比較軟體


Beyond Compare
https://www.scootersoftware.com


Hex Editor


HxD
https://mh-nexus.de


Git GUI


TortoiseGit
https://tortoisegit.org


網頁瀏覽器 (Web browser)


Firefox
https://www.mozilla.org


壓縮軟體


7-Zip
https://www.developershome.com/7-zip/


映像檔掛接工具


Virtual CloneDrive
https://www.elby.ch/


檔案總管


Q-Dir
https://www.softwareok.com/