bloggerads

2015年2月13日 星期五

UEFI : Build Nt32 (UEFI 模擬器)

執行步驟:

1. 開啟 cmd
2. 切到UEFI source code 的根目錄
3. edksetup --nt32
4. build -p Nt32Pkg\Nt32Pkg.dsc -a IA32
5. build run

2014年12月30日 星期二

C# : virtual + override or (virtual) + new

關鍵字是用來決定哪些成員要使用子類別中的定義

//      A a = new B(); //virtuall - new example
//
//                     A  ------------->  print (Call A)
//                     |
//    ------------->   B                  print (Call B)
//   

namespace virtual_new
{
    public class A
    {
         public virtual void print() { Console.WriteLine("Call A"); } // virtual here is not necessary!
    }
    
    public class : A
    {
         public new void print() { Console.WriteLine("Call B");  }
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            A a = new B();
            a.print();  // Output: Call A
        }
    }
}

2014年12月10日 星期三

C++ Compiler : g++ 指令介紹

以下提供一些g++指令參數解釋,基本上就是用來生成執行檔/dll檔/obj檔

# Generate file.o

g++ -c file.cpp  

# Generate Assembly : file.s 

g++ -S file.cpp  

# Convert file.o to file_dll.dll (需要先把cpp檔做成obj檔再來轉成dll檔)

g++ -shared file.o -o file_dll.dll 

# Generate file.exe

g++ file.cpp  -o file 

# Generate file.exe from object and cpp files

g++ file.cpp  obj.o -o file 

# Generate main.exe from dll and cpp files

g++ file_dll.dll main.cpp -o main

# Alternative solution to generate main.exe from file_dll.dll  and main.cpp ,  -L. means current dir

g++ -L. -lfile_dll main.cpp -o main 

# Show all the warning
gcc -Wall -o main main.c

# Warning as error
gcc -Wall -Werror -o main main.c

2014年11月7日 星期五

ATA: Security Erase flow

ATA 清理硬碟的command有兩種:
  1. Trim command: 06h
  2. Security Erase command: F4h
最安全的方式是對碟機下security erase command, 才可以保證把舊硬碟送人後資料不會被扒出來。

這邊介紹security erase command flow, 往往市面上的軟體都需要熱插拔碟機,但這邊提供的方法不需要,因此可以適用在PCIe AHCI SSD上

步驟如下:

1. 用ATA Set feature command通知碟機將Comreset 轉成Hardware reset (因為開機Bios會對你的碟機下Security freeze lock讓碟機變成state machine:frozen state。如果不這樣做就得先熱插拔碟機才能讓碟機回到state machine:not frozen, 才可以下security erase)

下法就是透過Set feature command (EFh), feature=0x90 and count=0x06,來disable software setting preservation
這樣之後我們下的Comreset才會被轉成 hardware reset, 讓碟機回到not frozen state


2. 接著透過 port reset 或 HBA reset 發出 COMRESET

3. 下security erase command之前要先設定密碼 (也就是提升權限才能下這個command,最後security erase也會一併erase掉這個密碼)

就是以下這三步驟:
  1. SECURITY SET PASSWORD: F1h
  2. SECURITY_ERASE_PREPARE: F3h
  3. SECURITY_ERASE_UNIT: F4h
通常SSD幾秒內就完成碟機的erase(基本上還是要看碟機廠商怎麼做),傳統轉盤的碟機就會比較久甚至有些是天荒地老的久XD.

2014年10月25日 星期六

C/C++一些經驗談

一些雜談,提供個人Coding 的經驗

1. 無窮迴圈的例子: 沒有注意宣告的變數範圍上限或邏輯錯誤而溢位產生無窮迴圈
  1. for (unsigned char x=0; x<=0xff; x++) ;
  2. for (unsigned char x=0xff; x>=0; x--) ;
2. 宣告雙指標的時機
用指標來管理記憶體,而雙指標(指標的指標)就是用指標管理指標
實務上在函式間傳遞指標的位址時, 就必需要傳遞指標的指標。舉個記憶體管理的例子,假如我有一串link list如下p1~p6,就可以透過雙指標pp start/pp tail指定鏈結串鏈其中一段的起始節點位址(&p2)和尾部節點位址的(&p5), 而PP start/ PP tail就是雙指標的型態

2014年9月26日 星期五

C 結構(structure) 的一些注意事項 (C89 / C99 / 對齊,Alignment)

1. 結構變數初值給定

首先,先宣告一個結構型態, 接下來再介紹兩種初值給定方法(C99/C89):
+  struct test
+  {
+    const char* cap;
+    unsigned char data_0;
+    int data_1;  
+  };

support C99 mode (現在新版本的GCC都有support C99 mode 的結構變數初值給定方法。 這種方法的優點是未來如果結構調整變數的位置,宣告變數初值不用跟著調整):

+  struct test node = 
+  {
+      .cap="first", 
+      .data_0=10,
+      .data_1=5
+  };

以下是傳統(C89 mode)的寫法 (很可惜的是目前Watcom C版本(1.9)尚未支援C99 mode結構變數的初值給定方法),所以還是用舊有的寫法:

+  struct test node = 
+ {
+      "first",   // cap
+      10,       // data_0
+      5          // data_1
+  };

2. 結構內變數對齊(alignment)的問題

2014年7月25日 星期五

在DOS下寫程式常會用到的中斷 (to DOS愛好者)

現在還在用Dos的人其實不少只是比較不顯眼 :P ,就我知道幾乎所有的x86系統在開發過程中,還是以Dos tool來驗證chipset問題。(畢竟誰會這麼閒重新在UEFI shell上開發以前在Dos下沒問題的程式XD)

我也是Dos的愛用者,開發過的Dos測試程式比在其他作業系統上的測試程式多得多,寫程式的功力也是從這邊開始練起。

基本上使用Watcom若是default使用protect mode, 那麼寫Dos程式幾乎跟在windows上感覺差不多,但是Watcom可以透過inline assembly神不知鬼不覺地使用原始 real mode 的中斷。

由於中斷很多,這篇僅列出我常使用的中斷給大家參考:

1. Access PCI configuration: INT 1A

提供範例函數來讀取PCI cfg data:
+
+  //Author: Martin Lee
+   const unsigned char PCI_FUNCTION_ID= 0xB1;
+   const unsigned char READ_CONFIG_DWORD 0x0A
+   
+   DWORD READ_PCI_CONFIG_DWORD(WORD BDF, WORD index)
+   {
+   
+      DWORD data;
+       __asm
+       {
+       // 1AB109 INT 1A - PCI BIOS v2.0c+ - READ CONFIGURATION WORD
+       // BH = bus number
+       // BL = device/function number (bits 7-3 device, bits 2-0 function)
+       // ECX = dword read
+       mov     BX,  BDF
+       mov     di,  index
+       mov     ah,  PCI_FUNCTION_ID
+       mov     al,  READ_CONFIG_DWORD
+       int     1ah
+       mov     data, ecx
+       }
+       return data;
+   }

2. 對碟機做讀寫: INT 13

3. 設定螢幕的顏色游標什麼的: INT 10 (Watcom有很多內建的函數可以使用,使用上方便又美觀), 像我這個介面就完全是呼叫Watcom 的library做的

(待續)