2018年10月22日 星期一

[VSCode] Code Snippet 程式碼片段


Visual Studio Code有提供Code Snippet的功能,讓使用者可以透過自己設定的關鍵字, 來快速產出程式碼的片段,提升開發的效率及方便性。

使用者程式碼片段


程式碼片段的設定在 "檔案 -> 喜好設定 -> 使用者程式碼片段"


Code Snippet可以分別對不同的程式語言做設置,讓使用者能針對不同的開發環境使用相應的
程式碼片段。此外,也能設置在全域環境使用的Code Snippet。



設定方法


Code Snippet是透JSON檔案來進行設置,以Python語言為例,設置Python Code Snippet的
JSON檔案就名為python.json。JSON的每個物件就代表一個程式碼片段。


以"Print to console"為例,prefix的值"prt"即為產出此Code Snippet的關鍵字。


prefix - 關鍵字
body - 產出內容
  $1, $2... - tab stop
  $0 - 游標最後停留處
  ${1:Label1}, ${2:Label2}... - 游標選取文字
description - 說明文字

2018年10月15日 星期一

檔案加密軟體 - AxCrypt


官方網址

https://www.axcrypt.net

測試系統

Windows 10

使用版本

AxCrypt2.1.1560.0

AxCrypt 為一套開源的檔案加密軟體,有釋出免費、付費及商用版本。
其免費版本就含有使用AES-128的檔案加解密功能。

安裝完成後,AxCrypt 會要求使用者提供帳密來做加解密使用。
接者,就可在所要加密的檔案上按下右鍵,選擇功能表中的AxCrypt來加密檔案。



AxCrypt會將加密過的檔案顯示在其列表中。



使用者也可以"右鍵 -> Remove from list but keep file secured" 來移除列表的檔案但保持其加密的狀態。



若要在其他裝置使用加密檔案,只需在該裝置上安裝AxCrypt,並使用同組帳密登入即可。

2018年9月13日 星期四

[Python] 套件管理程式 - pip


pip 是Python內建的套件管理的程式,用來安裝及管理Python的套件


pip install


安裝套件 pip install [MODULE_NAME]
安裝套件的特定版本 pip install [MODULE_NAME]==Version
更新套件至最新版本 pip install --upgrade [MODULE_NAME]
參考requirements.txt檔案安裝套件 pip install -r requirements.txt

pip show


顯示套件資訊 pip show [MODULE_NAME]

pip freeze


以requirements的形式顯示安裝的套件 pip freeze

pip uninstall


解除安裝套件 pip uninstall [MODULE_NAME]


保存套件的開發環境
可搭配virtualenv在乾淨的套件環境中使用

1. 使用pip freeze取出當前開發環境所使用的套件 python -m pip freeze > requirements.txt

2. 再用requirements.txt安裝開發環境的套件 python -m pip install -r requirements.txt

2018年8月27日 星期一

[Pythonic] 迴圈計數應用 enumerate()


在Python中,for迴圈可用來遍訪字串、串列等iterator,若要計算迴圈的數量,可在迴圈
中用counter來記錄。



seq = ["one", "two", "three", "four", "five"]
counter = 0
for e in seq:
  print (counter, e)
  counter += 1


1 one
2 two
3 three
4 four
5 five

另外還有比較Pythonic的寫法,就是使用enumerate (sequence, [start=0]) 函數。
sequence代入iterator,start則為起始位置。


seq = ["one", "two", "three", "four", "five"]
for idx, e in enumerate(seq):
  print (idx, e)


1 one
2 two
3 three
4 four
5 five


2018年8月7日 星期二

建置 Windows Debugger(WinDbg) 環境 via USB 3.0 cable

本篇文章參考
Debugging Tools for Windows

Preparation

Host Computer (with USB3.0 port)
  - windows-10-sdk
Target Computer (with USB3.0 Debug port)
USB 3.0 Debug Cable (A-A crossover, no VBus)

Setup Target Computer

1. 使用UsbView.exe來確認該port是否能Debug(Is Port Debug Capable: yes)。

2. 紀錄該port的xHCI Controller的Bus、Device和Function的number。

3. 使用系統管理員權限開啟command prompt,並輸入以下命令
bcdedit /debug on
bcdedit /dbgsettings usb targetname:TargetName
TargetName為自行命名。

4. 將step2 所記下的資訊帶入以下命令
bcdedit /set "{dbgsettings}" busparams b.d.f

5. 重新開機。

Setup Host Computer

1. 安裝Windows SDK

2. 安裝WinDbg的USB driver
(預設路徑:C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\usb)


使用WinDbg

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

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

3. 標籤頁切換到"USB",並輸入Setup Target Computer step 3的TargetName


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


設定Symbols

https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/symbol-path

.sympath
設定Debugger所要參考的symbol file的位置。

.sympath srv*c:\MyServerSymbols*https://msdl.microsoft.com/download/symbols 使用Microsoft Public Symbol Server https://msdl.microsoft.com/download/symbols 所提供的symbol並把local端的c:\MyServerSymbols 用來存放symbols。

或使用 "File" > "Symbol File Path ..." (Ctrl+S) 來設定。


2020/06/02 Note
最近嘗試用這樣的方式設symbol path會遇到ERROR_INTERNET_CANNOT_CONNECT無法 load symbol的情況。
所以參考how-to-specify-symbol-file-locations-from-the-command-line的方式,將
_NT_SYMBOL_PATH設成系統的環境變數,就能正常load到symbol。


Modules

lm
列出已載入的Modules

m PATTERN - 指定符合PATTERN的Module
ex: lm m a* 為列出所有a開頭的Modules

.reload
重新從Symbol Path載入Modules


2018年7月24日 星期二

[ACPI] ASL Operator


本篇文章參考
ACPI Specification Version 6.2 (Errata A)
ACPI Specification Version 5.1 (Errata B)
參考章節為CH19 ACPI Source Language(ASL) Reference

Add

Add(Addend1, Addend2, Result) => Integer
將Addend1與Addend2相加存放到Result。

CreateWordField

CreateWordField(SourceBuffer, ByteIndex, WordFieldName)
在SourceBuffer的ByteIndex Offset建立一個Word長度且名為WordFieldName的Object。

DerefOf

DerefOf(Source) => Object
回傳Source所Reference的Object。

Device

Device(DeviceName) {TermList}
建立一個名為DeviceName的Object,用以代表processor、bus或device等硬體。並會開啟一個name scope。

EISAID

EISAID(EisaIdString) => DWordConst
將形式為"UUUNNNN"(U為大寫字母,N為十六進位數字)的文字字串轉換為4 byte長度的EISA ID數字編碼。

Field

Field (RegionName, AccessType, LockRule, UpdateRule) {FieldUnitList}
可用以代表RegionName中的某段區塊資料。
AccessType為預設存取寬度,可為AnyAcc、ByteAcc、WordAcc、DWordAcc及QWordAcc。
FileUnitList的Entry可為下列幾種形式,
FieldUnitName, BitLength - Field Unit所使用的命名與長度。Field Unit用以代表某段資料。
Offect (ByteOffset) - 下個Field Unit的Offset。

Include

Include(FilePathName)
引入其他ASL File。

Index

Index (Source, IndexNum, Destination) => ObjectReference
取得 Source 中第 IndexNum項的 Reference。
如 Source為 Buffer,Index會回傳第IndexNum的byte的Reference。
如 Source為 String,Index會回傳第IndexNum的字元的Reference。
如 Source為 Package,Index會回傳第IndexNum的Object的Reference。

Method

Method (MethodName, NumArgs, SerializeRule, SyncLevel, ReturnType, ParameterTypes) {TermList}
建立一個名為MethodName的control method,並會開啟一個name scope。
NumArgs代表Method的參數數量。為optional如不使用則代表此Method不使用參數,而最多可使用7個參數,參數分別reference到Arg0 - Arg6。
SerializeRule可填為Serialized或NotSerialized,Serialized代表此Method不會被其他thread同時存取,用以防止產生相同namespace object。若沒有指定則視為NotSerialized。

Name

Name(ObjectName, Object)
建立名為ObjectName的Object,並且references到Object。

OperationRegion

OperationRegion(RegionName, RegionSpace, Offset, Length)
宣告名為RegionName的Operation Region,配合Field Object用以讀取系統硬體空間。

PowerResource

PowerResource(ResourceName, SystemLevel, ResourceOrder) {ObjectList}
宣告名為ResourceName的power resource。

Scope

Scope(Location) {ObjectList}
宣告名為Location的namespace,並且ObjectList以Location為參考namespace。
或將目前namespace切換成已存在的namespace。


Beyond BIOS Note - Ch1 Introduction

Chapter 1 Introduction


The suddenness of the leap from hardware to software cannot but produce a period of anarchy and collapse, especially in the developed countries.
- Marshall McLuhan

UEFI - UEFI is about booting, or passing control to successive layer of control

PI - PI describes the phase of control from the platform reset and into the success phase of operation

UEFI在於提供介面來讓其他應用來使用系統服務,而PI則在於規劃系統韌體的流程。


2018年7月17日 星期二

[ACPI] Device Configuration


本篇文章參考
ACPI Specification Version 6.2 (Errata A)
ACPI Specification Version 5.1 (Errata B)
參考章節為CH6 Device Configuration

OSPM 會根據 Configuration Object去管理操作相關的 Device,其大致分為幾個類型:

Device identification objects

_ADR (Address)

提供OSPM此裝置在所屬的Bus上的Address。
Bus   Address Encoding  
PCI   High word - Device Number, Low word - Function Number  
(ex: device 3, function 2 會是 0x00030002)  
若要參照device的所有function,Function Number則使用0xFFFF  
USB Ports   Port Number (1-n )  

Device configuration objects

_DIS (Disable)

用來 disable device。

_PRT (PCI Routing Table)

回傳包含PCI interrupt mapping package列表的package。

Device insertion and removal objects

_RMV (Status)

通知OSPM此裝置是否隨時都為可移除。

_STA (Status)

回傳裝置的狀態,其狀態可為enabled、disabled和removal。
若裝置的Object沒有_STA,則視為設起所有的return bit(present、enabled、shown in UI and functioning)。

Other Objects and Control Methods


_INI (Init)

用來對裝置進行特定的初始化操作,在OSPM加載description table時執行,會根據_STA來決定_INI是否及如何執行。


2018年7月12日 星期四

EDK常見的Macro - CR

在追EDK的Source Code時,會常看見Macro CR()的使用,其意義在於利用某個struct成員的記憶體位址,來獲得該struct的起始記憶體位址, 以此可以在記憶體操作上做更靈活的運用, 通常用於EFI Protocol。


#define CR (Record, TYPE, Field) \
  ((TYPE *) ((CHAR8 *) (Record) - (CHAR8 *) &(((TYPE *) 0)->Field)))


Record - Struct成員的記憶體位址
TYPE - Struct所宣告的型態
Field - Struct成員使用的名稱


CR拆解


1. &(((TYPE *) 0)->Field)
取得Struct成員在Struct中的offset

2. (CHAR8 *) (Record) - (CHAR8 *) &(((TYPE *) 0)->Field)
用Struct成員的實際記憶體位址減去Offset,就可得到Struct的記憶體位址

3. (TYPE *) ((CHAR8 *) (Record) - (CHAR8 *) &(((TYPE *) 0)->Field))
再轉型回Struct的宣告型態


2018年7月8日 星期日

使用Visual Studio Community 2017/2019 Build EDK2 on Windows

參考來源
https://github.com/tianocore/tianocore.github.io/wiki/Windows-systems
https://github.com/tianocore/tianocore.github.io/wiki/UDK2018#how-to-build-UDK2018

Source

Visual Studio Community 2017
https://visualstudio.microsoft.com

UDK2018
https://github.com/tianocore/edk2/releases/tag/vUDK2018

Basetools-Win32
https://github.com/tianocore/edk2-BaseTools-win32

Python2.7
https://www.python.org/

Cygwin
https://www.cygwin.com/

NASM
https://www.nasm.us/

iASL
https://acpica.org/downloads/binary-tools

Environment Variable

PYTHON_HOME - Python安裝路徑 (預設C:\Python27)
CYGWIN_HOME - Cygwin安裝路徑 (預設C:\cygwin64)
EDK_TOOLS_BIN - Basetools-Win32路徑
NASM_PREFIX - NASM路徑
IASL_PREFIX - iASL路徑

target.txt

ACTIVE_PLATFORM
指定要build的DSC file的路徑。Ex: Nt32Pkg/Nt32Pkg.dsc。
(build -p Nt32Pkg/Nt32Pkg.dsc)

TARGET
指定build target。Ex: DEBUG RELEASE。
(build -b DEBUG)

TARGET_ARCH
指定使用架構。Ex: IA32 X64。
(build -a X64)

TOOL_CHAIN_TAG
指定Tool Chain的Tagname。Ex: VS2017。
(build -t VS2017)

Setup Reference

Directory Structure

C:\
├ ─ ─ ─ Python27
└ ─ ─ ─ cygwin64
└ ─ ─ ─ Program Files (x86)
    └ ─ ─ ─ Microsoft Visual Studio
        └ ─ ─ ─ 2017
            └ ─ ─ ─ Community

(WORKSPACE)
├ ─ ─ ─ UDK2018
└ ─ ─ ─ edk2-BaseTools-win32
└ ─ ─ ─ iasl-win-20180629
└ ─ ─ ─ nasm-2.13.03
└ ─ ─ ─ (Build-UDK2018)

Build-UDK2018.bat

@echo off

set PYTHON_HOME=C:\Python27
set CYGWIN_HOME=C:\cygwin64

set EDK_TOOLS_BIN=%cd%\edk2-BaseTools-win32
set NASM_PREFIX=%cd%\nasm-2.13.03\
set IASL_PREFIX=%cd%\iasl-win-20180629\

cd .\UDK2018

call edksetup.bat
cmd

Run Nt32Pkg

1. Build the Nt32Pkg
build -p Nt32Pkg/Nt32Pkg.dsc -t VS2017 -a X64

2. Run the Nt32 emulation
build -t VS2017 -a X64 run


2019/04/08 Note:
在更新完EDK的source code之後,會出現build failed ...\CryptoPkg\Library\OpensslLib\OpensslLibCrypto.inf(31): error 000E: File/directory not found in workspace
  ...\CryptoPkg\Library\OpensslLib\openssl\e_os.h

參考檔案 CryptoPkg\Library\OpensslLib\OpenSSL-HOWTO.txt 需加入OpenSSL submodule

在已擁有local source且未加入submodule的情況下,加入OpenSSL submodule git submodule update --init --recursive

若還未擁有local source,可直接使用"--recursive" flag git clone --recursive https://github.com/tianocore/edk2

2020/08/09 Note:
要build最新 edk2 EmulatorPkg,basetool部分需要重新rebuild
且需使用VC2019,使用VC2017會build不過


2021/10/06 Note:
在另一台電腦重新架環境,並rebuild BaseTools的時候會遇到 assert.h
找不到的問題。後來才發現 %include%的 path有問題,原因是那台電腦中安裝的
WDK可能造成在設定 SDK path的時候出問題,後來解除安裝 WDK就能避開這個問題。