pretty code

2017年12月27日 星期三

Golang LeetCode

太久沒解就會忘記設定專案細節

1. 使用 Golang
2. foder name and package is 題號
3. In GOPATH, type "go test LeetCode/LeetCode_00728"
4. Benchmark, type "go test LeetCode/LeetCode_00278 -bench=."
src 
    - LeetCode
               - LeetCode_00728
                                - LeetCode_00728.go
                                - LeetCode_00728_test.go    

2017年12月11日 星期一

Windows 工作排程器 command-line

雖然我們可以用 GUI 界面去設定工作排程
但這樣的方式不太容易自動化

幸好,Windows  有一個指令 schtasks
我們可以利用它來自動化建立排程

Examples:
    SCHTASKS
    SCHTASKS /?
    SCHTASKS /Run /?
    SCHTASKS /End /?
    SCHTASKS /Create /?
    SCHTASKS /Delete /?
    SCHTASKS /Query  /?
    SCHTASKS /Change /?
    SCHTASKS /ShowSid /?


不過如果要執行的是批次檔,常會遇到工作路徑不對的問題
這時候我們有 2 種解決方式

1. 照 help 說明,傳參數進去,可惜我不管如何排列組合,總是不成功

    ==> 檔案路徑中的空白可使用兩組引號,一組
        用於 CMD.EXE,另一組用於 SchTasks.exe。用於 CMD 的外部引號
        必須是雙引號; 內部引號則可以是單引號或
        逸出雙引號:
        SCHTASKS /Create
           /tr "'c:\program files\internet explorer\iexplorer.exe'
           \"c:\log data\today.xml\"" ...


2. 利用內建變數,切換磁碟機與資料夾路徑

@echo off

%~d0
cd %~dp0


搞定收工,方法 1 有空再來看怎麼回事?

2017年11月7日 星期二

x86 Assembly Intel to AT&T

最近工作需要使用人家寫好的組語去控制 HW
我需要的是 AT&T 語法
不過因為對方不熟 AT&T 語法
故需轉換對方寫的 Intel 語法

本想直接人工轉換,但又怕不懂組語的我做白工
上網搜尋了一下,找到一個專案 ta2as
試了一下,只要調整來源檔輸出,就能順利的轉出 AT&T 語法
除了 mox 指令 找不到改用 movb 以外(有誰可以告訴我有 mox 指令 嗎?)
使用起來感覺沒什麼問題

另外,有參考 purpose 網友的 PTT 文章 ,好確認 ta2as 工具是否有轉錯

題外話,很久以前就想學組語,不過一直抽不出時間
brianhsu 網友有另外一篇 PTT 文章,推薦學組語的參考書籍
記錄一下當作備忘

2017年11月1日 星期三

UEFI Shell script format

UEFI Shell script 的寫法跟 Windows Batch 差不多
其副檔名為 "nsh"

文字檔編碼可以使用 UCS-2 or ASCII

不過因為 UEFI Shell 預設編碼為 UCS-2
如果將 Shell Command 導向到 UEFI C Application 產生的 ASCII Log 時,就會產生 error

此時只要簡單將 ">" 改成 ">a" 即可
a 就是 ASCII 的意思

2017年10月26日 星期四

UEFI Shell

最近需要 pass Shell Env to my shell script
原本以為是很難的事
幸好 UEFI 在 C Lib 中就提供了 setenv 函數

不過事情不是憨人想的那樣
呼叫此 function 會失敗
從 errno 中看到失敗值是 45 表示 unsupport

沒關係除了 setenv,我們還有二個方式
1. UefiShellLib 的 ShellSetEnvironmentVariable(後來才發現 setenv 也是 call 它)
2. 使用 SHELL PROTOCOL

很不幸的 2 個方式都會 gg,回傳的值是 3  (EFI_UNSUPPORTED)
此時直覺是 INF 設定有問題,調整老半天還是搞不定

後來換了 UEFI Shell 後,事情總算解決了
不過此時又跑出一個新的 question

EdkShellBinPkg 還有 ShellBinPkg 裡面都有 Shell Binary
為什麼一個可以成功,一個卻不行
過了一個禮拜終於在 官網 github 找到答案

簡單來說 EdkShellBinPkg 是 v1.0 的 Shell Spec
ShellBinPkg 是 v2.0 的 Shell Spec
我們從下圖的註解便可以得知
需要 UEFI Shell 2.0 的環境才能支援


/**
  set the value of an environment variable

This function changes the current value of the specified environment variable. If the
environment variable exists and the Value is an empty string, then the environment
variable is deleted. If the environment variable exists and the Value is not an empty
string, then the value of the environment variable is changed. If the environment
variable does not exist and the Value is an empty string, there is no action. If the
environment variable does not exist and the Value is a non-empty string, then the
environment variable is created and assigned the specified value.

  This is not supported pre-UEFI Shell 2.0.

  @param EnvKey                 The key name of the environment variable.
  @param EnvVal                 The Value of the environment variable
  @param Volatile               Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).

  @retval EFI_SUCCESS           the operation was completed sucessfully
  @retval EFI_UNSUPPORTED       This operation is not allowed in pre UEFI 2.0 Shell environments
**/
EFI_STATUS
EFIAPI
ShellSetEnvironmentVariable (
  IN CONST CHAR16               *EnvKey,
  IN CONST CHAR16               *EnvVal,
  IN BOOLEAN                    Volatile
  )
{
  //
  // Check for UEFI Shell 2.0 protocols
  //
  if (gEfiShellProtocol != NULL) {
    return (gEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));
  }

底下分別是 EdkShellBinPkg 和 ShellBinPkg 的 Shell version information







2017年10月16日 星期一

get callback's return value inside synchronous function

最近在幫別人改 Node.js 的 code
由於他的寫法是同步,但是又使用 callback 方式

原本以為在改架構時,可以簡單的改成 return 形式
但卻撞到不知名的 bug
試了一下才知道在 callback 中的 reurn 不等於呼叫 callback 的 function 的 return
如果直接 return,此時的 var a 會是 undefined

抱歉,不熟 Node.js 不知要怎麼形容
記錄一下,以後再來找答案


function test(callback) {
    console.log("in test");

    var usage = Math.floor((Math.random() * 100) + 1);

    if ((usage%2) == 0) {
        callback(true);
    } else {
        callback(false);
    }

    console.log("exit test");
}

function b() {
    console.log("in b");

    var res;
    test(function(result) {
        console.log("in test callback", result);
        /*
        if (result) {
            return true;
        } else {
            return false;
        }
        */
        res = result;
    });

    console.log("after test");
    return res;
}

var a = b();

console.log("after b");
console.log(a);

2017年10月2日 星期一

Golang - cross complie

原本測試 Linux 的電腦最近都用來跑 UEFI
由於不需要鍵盤,故臨時要用 Linux 很不方便
再加上該 Linux 並沒有安裝 Golang,故也沒有相關的設定

幸好,Golang 支援 cross complie
在我工作的 Windows 電腦上
只要簡單設定幾個環境變數
就可以順利的 build 出跨平台 binary

set GOOS=linux
set GOARCH=amd64
go build