2024年7月14日 星期日

Notes for JEDEC Standard No. 84-B51A - CH1~CH5


eMMC是一種管理型記憶體(managed memory ),專為儲存代碼和資料而設計,具有低功耗的特點,非常適合行動裝置使用。
eMMC 通訊匯流排: eMMC 使用一個包含11個訊號的匯流排進行通信,包括Clock、Data Strobe(資料同步訊號)、1位元Command、8位元Data Bus。

Clock: 支援的時脈頻率範圍為0到200MHz。
Data Bus: eMMC支援三種不同的資料匯流排寬度模式:1位(預設)、4位和8位。這些模式允許根據需要調整資料傳輸速度和並行性。

eMMC 的優勢:
低功耗: eMMC 設計具有低功耗特性,適合在電池供電的行動裝置中使用。 間接記憶體存取: eMMC 透過一個獨立的控制器來實現間接記憶體存取。這意味著設備可以在不依賴主機軟體的情況下處理後台記憶體管理任務。這種特性簡化了主機系統上的快閃記憶體管理層。


在 eMMC裝置中,記憶體定址和容量由定址模式和磁區大小決定。主要模式:byte addressing與sector addressing。
byte addressing:使用 32 位元直接定址單一位元組,最高支援 2GB,對於更大容量,這種定址模式不切實際。
sector addressing:對於大於 2GB 的設備,eMMC 切換到sector addressing。在這種模式下,定址指的是磁區而不是單一位元組。磁區大小通常為 512 位元組或 4 KB。
512B:理論上可以支援最高 2 TiB 的記憶體,2^32 個磁區 × 512 位元組/磁區 = 2^32 × 2^9 位元組 = 2^41 位元組 = 2 TiB。實際上,最大容量通常被認為是 256GB。這個差異是由於控制器複雜性、邏輯到實體位址映射、磨損均衡和壞區塊管理等開銷所致,這些都會消耗額外的資源。
4 KB 區:理論上可以支援最高2^32 個磁區 × 4 KB/磁區 = 2^32 × 2^12 位元組 = 2^44 位元組 = 16 TiB
控制器複雜性: 隨著磁區大小的減少和容量的增加,邏輯到實體映射、磨損均衡和壞區塊管理的複雜性和開銷也隨之增加。

eMMC 訊號線的解釋
CLK(時脈訊號):控制資料傳輸的時序。每個時脈週期觸發指令線上傳輸一位數據,資料線則傳輸一位(1x)或兩位數(2x)數據,視工作模式而定。時脈頻率可以從零到設備支援的最大頻率變化(0~200MHz)。

Data Strobe(資料同步訊號):eMMC 設備生成,用於 HS400 模式下的資料同步。Data Strobe的頻率與時脈訊號(CLK)頻率相符。對於資料輸出,每個週期可以傳輸兩位資料(一個在正邊緣,一個在負邊緣)。對於 CRC 狀態回應和指令回應輸出(僅在 HS400 enhanced strobe mode下),CRC 狀態和指令回應僅在正邊緣上鎖存,負邊緣則不考慮。

CMD(命令訊號):CMD 訊號是一個雙向頻道,用於從 eMMC host發送命令到 eMMC 設備,並接收設備的回應。它有兩種工作模式:
Open-Drain Mode: 初始化期間使用,以最小化雜訊並確保訊號完整性。這種模式對訊號完整性問題有較高的容忍度。
Push-Pull Mode: 用於快速指令傳輸,此模式下訊號主動驅動至高或低電平,實現邏輯狀態之間的快速可靠轉換。

DAT0-DAT7:是雙向資料線,用於在Host和 eMMC 裝置之間傳輸資料。它們工作在Push-Pull Mode模式下,允許快速資料傳輸,因為在任一時刻只有設備或主機驅動這些訊號。
通電或重置後,最初只有 DAT0 用於資料傳輸。然而,資料匯流排寬度可以擴展到包括 DAT0-DAT3(4 位元模式)或 DAT0-DAT7(8 位元模式),以增加資料吞吐量。 eMMC 裝置為 DAT1-DAT7 提供內部上拉電阻,當裝置進入對應的寬匯流排模式(4 位元或 8 位元)時,這些上拉電阻會被斷開。





bus protocol
在裝置上電重設後,主機必須透過特定的訊息初始化裝置。每個訊息由以下幾種標記組成:
Command:CMD是從主機發送到設備。命令透過 CMD 線傳輸。
Response:Response是設備作為先前接收到的命令的回復發送給主機的標記。響應同樣透過 CMD 線串行傳輸。
Data:Data可以從裝置傳輸到主機,也可以從主機傳輸到裝置。資料透過data bus傳輸,資料線的數量可以是 1(DAT0)、4(DAT0-DAT3)或 8(DAT0-DAT7)。對於每條資料線,資料傳輸可以是單一資料速率(每個時脈週期傳輸一位元)或雙資料速率(每個時脈週期傳輸兩位元)。


eMMC 指令和資料傳輸
命令發送一個資料塊後接 CRC 位元。讀取和寫入操作都允許單一或多個區塊的傳輸。多個區塊的傳輸會在 CMD 線上的停止命令發出後終止,類似於順序讀取。




寫入操作: 區塊寫入操作使用簡單DAT0傳輸忙碌訊號。




指令標記:每個指令標記前方有一個起始位元(‘0’)和一個結束位元(‘1’),總長度為 48 位元。每個標記都受到 CRC 位元的保護,以便偵測傳輸錯誤,如果發生錯誤,操作可以重複。


回應標記:根據其內容有五種編碼方案。標記長度為 48 位元或 136 位元。區塊資料的 CRC 保護演算法採用 16 位元 CCITT 多項式。這些規範確保 eMMC 設備與主機之間的資料和命令傳輸的可靠性和完整性。




    參考資料:
  • JESD84-B51A (Revision of JESD84-B51, February 2015)



2024年7月5日 星期五

Configuration of a minimal systemd setup in QEMU


本文記錄如何在 QEMU 環境中設定最小的 systemd init。基本上需要建立兩個必要的目標:sysinit.target 和 basic.target,以及用於登入的 getty@.service。
SRCROOT=/opt/armv7vet2hf/sysroots/
DSTROOT=initrd
SRCLIB=$SRCROOT/lib
DSTLIB=$DSTROOT/lib
SRCSYSDLIB=$SRCROOT/lib/systemd
DSTSYSDLIB=$DSTROOT/lib/systemd
SRCUSRLIB=$SRCROOT/usr/lib
DSTUSRLIB=$DSTROOT/usr/lib

rm -rf $DSTROOT
mkdir -p $DSTROOT/bin $DSTROOT/usr/lib $DSTROOT/lib/systemd/system $DSTROOT/etc/systemd/system

# copy libraries
cp -a $SRCLIB/libselinux.so* $DSTLIB
cp -a $SRCLIB/libmount.so* $DSTLIB
cp -a $SRCLIB/libaudit.so* $DSTLIB
cp -a $SRCLIB/libc.so* $DSTLIB
cp -a $SRCLIB/ld-linux-armhf.so* $DSTLIB
cp -a $SRCLIB/libblkid.so* $DSTLIB
cp -a $SRCLIB/libcap.so* $DSTLIB
cp -a $SRCLIB/libm.so* $DSTLIB
cp -a $SRCLIB/libpcre.so* $DSTLIB
cp -a $SRCLIB/libcap-ng.so* $DSTLIB

cp -a $SRCUSRLIB/libacl.so* $DSTUSRLIB
cp -a $SRCUSRLIB/libcrypt.so* $DSTUSRLIB
cp -a $SRCUSRLIB/liblzma.so* $DSTUSRLIB
cp -a $SRCUSRLIB/libattr.so* $DSTUSRLIB

cp -a $SRCSYSDLIB/systemd $DSTSYSDLIB
cp -a $SRCSYSDLIB/libsystemd-shared* $DSTSYSDLIB
install -m 555 busybox-build/busybox $DSTROOT/bin/


# Create basic.target
cat << EOF > $DSTSYSDLIB/system/basic.target
[Unit]
Description=Basic System
EOF

# Create sysinit.target
cat << EOF > $DSTSYSDLIB/system/sysinit.target
[Unit]
Description=System Initialization
DefaultDependencies=no
EOF

# Create getty@tty1.service
cat << EOF > $DSTSYSDLIB/system/getty@.service
[Unit]
Description=Getty on %I
ConditionPathExists=/dev/%I

[Service]
ExecStart=-/sbin/getty 115200 %I
Restart=always

[Install]
WantedBy=basic.target rescue.target
EOF


chmod 644 -R $DSTSYSDLIB/system/*

cat << EOF > $DSTROOT/init
#!/bin/busybox sh
## Mount essential filesystems
/bin/busybox mkdir -p /proc /sys /dev /home /run/systemd/journal /tmp /var /mnt /sbin /usr/bin /usr/sbin /etc/systemd/system/basic.target.wants /etc/systemd/system/rescue.target.wants /etc/systemd/system/default.target.wants
/bin/busybox --install -s
ln -sf /lib/systemd/systemd /sbin/init

## Set the path for BusyBox applets if using BusyBox
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev
mkdir -p /dev/pts
mount -t devpts none /dev/pts
## Ensure the getty@ttyAMA0.service file is linked in the correct place
ln -sf /lib/systemd/system/getty@.service /etc/systemd/system/basic.target.wants/getty@ttyAMA0.service
# create default password
echo "root:vnpTT1BZdW1/s:0:0:root:/root:/bin/sh" > /etc/passwd
echo "root:x:0:" > /etc/group

# Ensure systemd can find its units
ln -sf /lib/systemd/system/basic.target /lib/systemd/system/default.target

# Start systemd
exec /sbin/init
EOF
chmod +x $DSTROOT/init

fakeroot bash -c "cd linux && ./usr/gen_initramfs.sh ../$DSTROOT -o ../initrd-arm.img"



2024年5月5日 星期日

A pattern for state machine III - SM framework


科技始終來自人性。最近剛好看到別人寫的SM有點糟糕,於是想起自己之前寫的,感覺也是不夠直覺,於是改寫了一下。主要概念還是根據SM的定義。
    An abstract state machine is a software component that defines a finite set of states:
  • One state is defined as the initial state. When a machine starts to execute, it automatically enters this state.
  • Each state can define actions that occur when a machine enters or exits that state.
  • Each state can define events that trigger a transition.
  • A transition defines how a machine would react to the event, by exiting one state and entering another state.
所以就從需求先定義API,再來實作內容。首先想到的是需要一個API來初始化這個SM,於是就有sm_alloc()誕生,並回傳sm這個抽象結構,sm_free()是用來釋放該SM的(destroy)。
typedef void * sm;
sm sm_alloc(char *name, void *data);
int sm_free(sm s);

Each state can define actions that occur when a machine enters or exits that state. 這句化,建立了API int sm_state_add(sm s, int state, sm_fp enter, sm_fp exit),用以建立sm中的"狀態",並且綁定enter action與exit action。
typedef int(*sm_fp)(void *data);
int sm_state_add(sm s, int state, sm_fp enter, sm_fp exit);
int sm_state_del(sm s, int state);

Each state can define events that trigger a transitionA transition defines how a machine would react to the event, by exiting one state and entering another state.這句話中,建立了API int sm_event_add(sm s, int state, int event, int new_state, sm_fp action),用於建立狀態中的"事件",指定"新狀態",以及"事件對應的動作"。並且使用API int sm_run(sm s, int new_event)讓sm根據收到的"事件"執行。
int sm_event_add(sm s, int state, int event, int new_state, sm_fp action);
int sm_event_del(sm s, int state, int event);
int sm_run(sm s, int new_event);

根據One state is defined as the initial state. When a machine starts to execute, it automatically enters this state.,我們定義了API int sm_init_state_set(sm s, int state)用以設定"初始狀態"。
int sm_current_state(sm s);

至此所有API都齊全了,以下是sm.h,其中我用marco稍微讓這個API多存一些資訊。
#ifndef _SM_H_
#define _SM_H_

typedef void * sm; /**< State machine handle */
typedef int(*sm_fp)(void *data); /**< State machine function pointer */

sm sm_alloc(char *name, void *data); /**< Allocate a state machine */
int sm_free(sm s); /**< Free a state machine */

#define sm_state_add(s, state, enter, exit) _sm_state_add(s, state, #state, (sm_fp)enter, #enter, (sm_fp)exit, #exit)
int _sm_state_add(sm s, int state, const char *st_name, sm_fp enter, const char * ent_fname, sm_fp exit, const char * exit_fname); /**< Add a state */

int sm_state_del(sm s, int state); /**< Delete a state */
int sm_run(sm s, int new_event); /**< Run the state machine */

#define sm_event_add(s, state, event, new_state, action) _sm_event_add(s, state, event, #event, new_state, (sm_fp)action, #action)
int _sm_event_add(sm s, int state, int event, const char *ev_name, int new_state, sm_fp action, const char *action_fname); /**< Add an event */
int sm_event_del(sm s, int state, int event); /**< Delete an event */
int sm_init_state_set(sm s, int state); /**< Set the initial state */
int sm_current_state(sm s); /**< Get the current state */
int sm_dump_state(sm s); /**< Dump the state machine */
#endif
接下來要解釋一下每個API的時作內容,首先是sm sm_alloc(char *name, void *data),我的想法是不限制有多少"狀態",所以要用link list去串SM中的每一個狀態,每個狀態都去串始於自己的"事件",為了在刪除"狀態"時,也能刪除指向該"狀態"的"事件",於於是我在每個"狀態"中多存了"被指到的事件(pointed_event_ll)"。
#include "sm.h"
#include "list.h"

struct sm_state {
    struct list_head state_ll;
    int state;
    const char *st_name;
    sm_fp enter;
    const char *ent_fname;
    sm_fp exit;
    const char *exit_fname;
    struct list_head event_ll;
    struct list_head pointed_event_ll;
};

struct _sm {
    void *v;
    char *name;
    struct list_head state_ll;
    struct sm_state *cur_sm_st;
    pthread_mutex_t mutex;
};

sm sm_alloc(char *name, void *data)
{
    struct _sm *sm;
    sm = (struct _sm *) malloc(sizeof(struct _sm));
    if (!sm) {
        sm_pr_err("malloc failed\n");
        return NULL;
    }
    sm->v = data;
    sm->name = strdup(name);
    sm->cur_sm_st = NULL;
    if (!sm->name) {
        sm_pr_err("malloc name failed\n");
        free(sm);
        return NULL;
    }
    INIT_LIST_HEAD(&apm;sm->state_ll);
    pthread_mutex_init(&sm->mutex, NULL);
    return sm;
}

int sm_free(sm s)
{
    struct _sm *sm = (struct _sm *) s;
    struct sm_state *sm_st, *tmp_sm_st;
    struct sm_event *sm_ev, *tmp_sm_ev;
    if (!sm) {
        return -1;
    }
    list_for_each_entry_safe(sm_st, tmp_sm_st, &sm->state_ll, state_ll) {
        list_for_each_entry_safe(sm_ev, tmp_sm_ev, &st->event_ll, event_ll) {
            list_del(&sm_ev->event_ll);
            free(sm_ev);
        }
        list_del(&sm_st->state_ll);
        free(sm_st);
    }
    pthread_mutex_destroy(&sm->mutex);
    free(sm->name);
    free(sm);
    return 0;
}

接著要說一下int sm_state_add(sm s, int state, sm_fp enter, sm_fp exit),其實只要判斷不存在要建立的state,剩下就是把資訊存到struct sm_state *而已,而sm_state_del()就是把對應的event都刪除後,釋放對應的resource。
static struct sm_state *sm_get_sm_state(sm s, int state)
{
    struct _sm *sm = (struct _sm *) s;
    struct sm_state *sm_st;
    list_for_each_entry(sm_st, &sm->state_ll, state_ll) {
        if (sm_st->state == state) {
            return st;
        }
    }
    return NULL;
}

int _sm_state_add(sm s, int state, const char *st_name, sm_fp enter, const char * ent_fname, sm_fp exit, const char *exit_fname)
{
    struct _sm *sm = (struct _sm *) s;
    struct sm_state *sm_st;
    if (!sm) {
        sm_pr_err("invalid argument\n");
        return -1;
    }
    // if state is already exist, return -1
    sm_st = sm_get_sm_state(s, state);
    if (sm_st) {
        sm_pr_err("state exist\n");
        return -1;
    }

    sm_st = (struct sm_state *) malloc(sizeof(struct sm_state));
    if (!sm_st) {
        sm_pr_err("malloc failed\n");
        return -1;
    }
    sm_st->state = state;
    sm_st->st_name = st_name;
    sm_st->enter = enter;
    sm_st->ent_fname = ent_fname;
    sm_st->exit = exit;
    sm_st->exit_fname = exit_fname;
    INIT_LIST_HEAD(&sm_st->event_ll);
    INIT_LIST_HEAD(&sm_st->pointed_event_ll);
    list_add_tail(&sm_st->state_ll, &sm->state_ll);
    return 0;
}

int sm_state_del(sm s, int state)
{
    struct _sm *sm = (struct _sm *) s;
    struct sm_state *sm_st;
    struct sm_event *sm_ev, *tmp_sm_ev;
    if (!sm) {
        sm_pr_err("invalid argument\n");
        return -1;
    }

    sm_st = sm_get_sm_state(s, state);
    if (!sm_st) {
        sm_pr_err("state is not exist\n");
        return -1;
    }

    list_for_each_entry_safe(sm_ev, tmp_sm_ev, &sm_st->event_ll, event_ll) {
        list_del(&sm_ev->event_ll);
        free(sm_ev);
    }
    list_for_each_entry_safe(sm_ev, tmp_sm_ev, &sm_st->pointed_event_ll, pointed_event_ll) {
        sm_st = sm_ev->sm_state;
        list_del(&sm_ev->pointed_event_ll);
    }
    list_del(&sm_st->state_ll);
    free(sm_st);
    return 0;
}

int sm_event_add(sm s, int state, int event, int new_state, sm_fp action);要先判斷"狀態"與"新狀態"存在,且"事件"不存在,接著把該"事件"串到該"狀態"去。
static struct sm_event *sm_get_sm_event(struct sm_state *st, int event)
{
    struct sm_event *sm_ev;
    list_for_each_entry(sm_ev, &st->event_ll, event_ll) {
        if (sm_ev->event == event) {
            return sm_ev;
        }
    }
    return NULL;
}

int _sm_event_add(sm s, int state, int event, const char *ev_name, int new_state, sm_fp action, const char *action_fname)
{
    struct _sm *sm = (struct _sm *) s;
    struct sm_state *sm_st, *new_sm_st;
    struct sm_event *sm_ev;
    if (!sm) {
        sm_pr_err("invalid argument\n");
        return -1;
    }
    sm_st = sm_get_sm_state(s, state);
    if (!sm_st) {
        sm_pr_err("state is not exist\n");
        return -1;
    }

    sm_ev = sm_get_sm_event(sm_st, event);
    if (sm_ev) {
        sm_pr_err("event is already exist\n");
        return -1;
    }

    new_sm_st = sm_get_sm_state(s, new_state);
    if (!new_sm_st) {
        sm_pr_err("new state is not exist\n");
        return -1;
    }

    sm_ev = (struct sm_event *) malloc(sizeof(struct sm_event));
    if (!sm_ev) {
        sm_pr_err("malloc failed\n");
        return -1;
    }
    sm_ev = (struct sm_event *) malloc(sizeof(struct sm_event));
    if (!sm_ev) {
        sm_pr_err("malloc failed\n");
        return -1;
    }
    sm_ev->sm_state = sm_st;
    sm_ev->event = event;
    sm_ev->ev_name = ev_name;
    sm_ev->new_sm_state = new_sm_st;
    sm_ev->action = action;
    sm_ev->action_fname = action_fname;
    list_add_tail(&sm_ev->event_ll, &sm_st->event_ll);
    list_add_tail(&sm_ev->pointed_event_ll, &new_sm_st->pointed_event_ll);
    return 0;
}

int sm_event_del(sm s, int state, int event)
{
    struct _sm *sm = (struct _sm *) s;
    struct sm_state *sm_st;
    struct sm_event *sm_ev;
    if (!sm) {
        sm_pr_err("invalid argument\n");
        return -1;
    }
    sm_st = sm_get_sm_state(s, state);
    if (!sm_st) {
        sm_pr_err("state is not exist\n");
        return -1;
    }
    sm_ev = sm_get_sm_event(sm_st, event);
    if (!sm_ev) {
        sm_pr_err("event is not exist\n");
        return -1;
    }
    list_del(&sm_ev->event_ll);
    free(sm_ev);
    return 0;
}

int sm_init_state_set(sm s, int state),其實就是找到,該"狀態",然後把SM的cur_sm_st指向它
int sm_init_state_set(sm s, int state)
{
    struct _sm *sm = (struct _sm *) s;
    struct sm_state *sm_st;
    if (!sm) {
        sm_pr_err("invalid argument\n");
        return -1;
    }
    sm_st = sm_get_sm_state(s, state);
    if (!sm_st) {
        sm_pr_err("state is not exist\n");
        return -1;
    }
    sm->cur_sm_st = sm_st;
    return 0;
}

最後是 int sm_run(sm s, int event),從cur_sm_st去找對應的"事件",如果找到,就執行離開該"狀態"的"動作",接著觸發該"事件"的"動作",最後設定"新狀態"為cur_sm_st,並執行新狀態的進入"動作"
int sm_run(sm s, int event)
{
    struct _sm *sm = (struct _sm *) s;
    struct sm_state *sm_st;
    struct sm_event *sm_ev;
    if (!sm) {
        sm_pr_err("invalid argument\n");
        return -1;
    }
    pthread_mutex_lock(&sm->mutex);
    if (!sm->cur_sm_st) {
        sm_pr_err("invalid stats\n");
        pthread_mutex_unlock(&sm->mutex);
        return -1;
    }

    sm_st = sm->cur_sm_st;
    sm_ev = sm_get_sm_event(sm_st, event);
    if (!sm_ev) {
        sm_pr_err("event is not exist\n");
        pthread_mutex_unlock(&sm->mutex);
        return -1;
    }
    if (sm->cur_sm_st->exit) {
        sm->cur_sm_st->exit(sm->v);
    }
    if (sm_ev->action) {
        sm_ev->action(sm->v);
    }
    sm->cur_sm_st = sm_ev->new_sm_state;
    if (sm->cur_sm_st->enter) {
        sm->cur_sm_st->enter(sm->v);
    }
    
    pthread_mutex_unlock(&sm->mutex);
    return 0;
}

剩下的僅是一些協助的API
int sm_current_state(sm s)
{
    struct _sm *sm = (struct _sm *) s;
    if (!sm) {
        sm_pr_err("invalid argument\n");
        return -1;
    }
    return sm->cur_sm_st->state;
}

int sm_dump_state(sm s)
{
    struct _sm *sm = (struct _sm *) s;
    struct sm_state *sm_st, *ori_sm_st;
    struct sm_event *sm_ev;
    if (!sm) {
        sm_pr_err("invalid argument\n");
        return -1;
    }
    list_for_each_entry(sm_st, &sm->state_ll, state_ll) {
        printf("state: %d/%p/%s\n", sm_st->state, sm_st, sm_st->st_name);
        printf("\tenter_fp: %p, enter_fname: %s\n", sm_st->enter, sm_st->enter?sm_st->ent_fname:"");
        printf("\texit_fp: %p, exit_fname: %s\n", sm_st->exit, sm_st->exit?sm_st->exit_fname:"");
        list_for_each_entry(sm_ev, &sm_st->event_ll, event_ll) {
            printf("\tevent: %d/%p/%s, new_state: %d/%p/%s, ev_fp:%p/%s\n", sm_ev->event, sm_ev, sm_ev->ev_name,
                            sm_ev->new_sm_state->state, sm_ev->new_sm_state, sm_ev->new_sm_state->st_name,
                            sm_ev->action, sm_ev->action?sm_ev->action_fname:"");
        }
        // pointed event is the event that point to this state
        list_for_each_entry(sm_ev, &sm_st->pointed_event_ll, pointed_event_ll) {
            ori_sm_st = sm_ev->sm_state;
            printf("\tpointed event: %d/%p/%s, from state: %d/%p/%s, ev_fp:%p/%s\n", sm_ev->event, sm_ev, sm_ev->ev_name,
                            ori_sm_st->state, ori_sm_st, ori_sm_st->st_name,
                            sm_ev->action, sm_ev->action?sm_ev->action_fname:"");
        }
    }
    return 0;
}



熱門文章