2023年1月1日 星期日

Table Of Content for tag "Linux - kernel"


Linux Kernel(1)- Linux Module簡介
Linux Modules(1.1)module parameters

Linux Kernel(2)- register char device
Linux Kernel(2.1)- MAJRO NUMBER RESERVED FOR DYNAMIC ASSIGNMENT

Linux Kernel(3)- procfs
Linux Kernel(3.1)- procfs之vector方式寫入
Linux Kernel(3.2)- procfs之symlink與mkdir

Linux Kernel(4)- seq_file
Linux Kernel(4.1)- seq_file之範例(fp/proc/devices.c)
Linux Kernel(4.2)- seq_file之single page

Linux Kernel(5)- ioctl

Linux Kernel(6)- miscdev

Linux Kernel(7)- timing
Linux Kernel(7.1)- timer
Linux Modules(7.2)- tasklet
Linux Modules(7.3)- work queue

Linux Kernel(8)- Notification
Linux Kernel(8.1)- Notifier機制剖析

Linux Kernel(9)- Kthread

Linux Kernel(10)- MTD/Memory Technology Device
Linux Kernel(10.1)- drivers/mtd/devices/mtdram.c
Linux Kernel(10.2)- mtd partitions
Linux Kernel(10.3)- Command line partition table parsing
Linux Kernel(10.3.1)- Command line partition table parsing for Kernel 4.19

Linux Kernel(11)- sysfs and device node
Linux Kernel(11.1)- sysfs and hotplug
Linux Kernel(11.2)- mdev.conf

Linux Kernel(12)- netfilter
Linux Kernel(12.1)- netfilter機制之初探

Linux Kernel(13)- syscall

Linux Kernel(14)- Kernel Synchronization
Linux Modules(14.1)- Read Copy Update

Linux Kernel(15)- Platform Devices
Linux Kernel(15.1)- platform_driver_register()之如何调用driver.probe()
Linux Kernel(15.2)- platform_device_register()之如何调用driver.probe()
Linux Kernel(15.3)- The Linux usage model for device tree data

Linux Kernel(16.1)- Network Device Driver, simple snull

Linux Kernel(17)- Device Tree
Linux Kernel(17.1)- Basic Device Tree syntax
Linux Kernel(17.2)- Common Device Tree API

Linux Kernel(18)- Virtual File System
Linux Kernel(18.1)- My First Filesystem
Linux Kernel(18.2)- SysCall mount

Linux Kernel(19)- General Purpose Input/Output
Linux Kernel(19.1)- /sys/class/gpio usage

Linux Kernel(20)- Input device
Linux Kernel(20.1)- Input device user program
Linux Kernel(20.2)- uinput module
Linux Kernel(20.3)- Creating an input device driver

Linux Kernel(21)- ID Allocation
Linux Kernel(21.1)- ID Allocation
Linux Kernel(21.2)- radix tree API
Linux Kernel(21.3)- radix tree implementation

Linux Kernel(22)- Linux Socket
Linux Kernel(22.1)- My Socket Domain and Protocol

Linux Kernel(23)- SKB


Linux Kernel(24)- Multimedia Card
Linux Kernel(24.1)- fdisk Multimedia Card

Linux Kernel(25)- USB(Universal Serial Bus)
Linux Kernel(25.1)- Gadget Configfs




2022年12月31日 星期六

Linux Kernel(19.1)- /sys/class/gpio usage


要能使用/sys/class/gpio就要先開啟相關kernel config如下, 如提示所說, 該ABI已經棄用, 改由character device /dev/gpiochipN取代, 不過這裡還是會簡單交代一下相關資訊, 作為紀錄.
  │ CONFIG_GPIO_SYSFS:                                                                             │
  │                                                                                                │
  │ Say Y here to add the legacy sysfs interface for GPIOs.                                        │
  │                                                                                                │
  │ This ABI is deprecated. If you want to use GPIO from userspace,                                │
  │ use the character device /dev/gpiochipN with the appropriate                                   │
  │ ioctl() operations instead. The character device is always                                     │
  │ available.                                                                                     │
  │                                                                                                │
  │ Symbol: GPIO_SYSFS [=y]                                                                        │
  │ Type  : bool                                                                                   │
  │ Prompt: /sys/class/gpio/... (sysfs interface)                                                  │
  │   Location:                                                                                    │
  │     -> Device Drivers                                                                          │
  │       -> GPIO Support (GPIOLIB [=y])                                                           │
  │   Defined at drivers/gpio/Kconfig:61                                                           │
  │   Depends on: GPIOLIB [=y] && SYSFS [=y]                                                       │                                   

在設定玩kernel config之後, 重新編譯kernel, 就可以看見"/sys/class/gpio"目錄, 裡面幾個重要的檔案,
export Userspace may ask the kernel to export control of a GPIO to userspace by writing its number to this file.
unexport Reverses the effect of exporting to userspace.

"echo 19 > /sys/class/gpio/export" 後, 目錄/sys/class/gpio/gpio19/就會被創建出來, 裡面會包含
direction reads as either "in" or "out"
value reads as either 0 (low) or 1 (high)
edge reads as either "none", "rising", "falling", or "both"
active_low reads as either 0 (false) or 1 (true)

此外還可以透過debugfs來檢查當前的GPIO設定
/ # cat /sys/kernel/debug/gpio
gpiochip3: GPIOs 499-499, parent: platform/basic-mmio-gpio.3.auto, sys_flash:

gpiochip2: GPIOs 500-501, parent: platform/basic-mmio-gpio.2.auto, sys_mci:
 gpio-500 (                    |cd                  ) in  lo
 gpio-501 (                    |wp                  ) in  lo

gpiochip1: GPIOs 502-509, parent: platform/basic-mmio-gpio.1.auto, sys_led:
 gpio-502 (                    |?                   ) out lo
 gpio-503 (                    |?                   ) out lo
 gpio-504 (                    |?                   ) out lo
 gpio-505 (                    |?                   ) out lo
 gpio-506 (                    |?                   ) out hi
 gpio-507 (                    |?                   ) out lo
 gpio-508 (                    |?                   ) out lo
 gpio-509 (                    |?                   ) out lo

gpiochip0: GPIOs 510-511, parent: platform/10000000.sysreg, 10000000.sysreg:

/ # ls -al /sys/class/gpio/
total 0
drwxr-xr-x    2 0        0                0 Jan  3 00:40 .
drwxr-xr-x   36 0        0                0 Jan  3 00:40 ..
--w-------    1 0        0             4096 Jan  3 00:40 export
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip499 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.3.auto/gpio/gpiochip499
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip500 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.2.auto/gpio/gpiochip500
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip502 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.1.auto/gpio/gpiochip502
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip510 -> ../../devices/platform/10000000.sysreg/gpio/gpiochip510
--w-------    1 0        0             4096 Jan  3 00:40 unexport

接下來export 510, 511, 然後在unexport 511, 設定510, 相關操作如下
/ # echo 510 > /sys/class/gpio/export
/ # echo 511 > /sys/class/gpio/export
/ # ls -al /sys/class/gpio/
total 0
drwxr-xr-x    2 0        0                0 Jan  3 00:40 .
drwxr-xr-x   36 0        0                0 Jan  3 00:40 ..
--w-------    1 0        0             4096 Jan  3 00:43 export
lrwxrwxrwx    1 0        0                0 Jan  3 00:42 gpio510 -> ../../devices/platform/10000000.sysreg/gpiochip0/gpio/gpio510
lrwxrwxrwx    1 0        0                0 Jan  3 00:43 gpio511 -> ../../devices/platform/10000000.sysreg/gpiochip0/gpio/gpio511
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip499 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.3.auto/gpio/gpiochip499
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip500 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.2.auto/gpio/gpiochip500
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip502 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.1.auto/gpio/gpiochip502
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip510 -> ../../devices/platform/10000000.sysreg/gpio/gpiochip510
--w-------    1 0        0             4096 Jan  3 00:40 unexport

/ # echo 511 > /sys/class/gpio/unexport
/ # ls -al /sys/class/gpio/
total 0
drwxr-xr-x    2 0        0                0 Jan  3 00:40 .
drwxr-xr-x   36 0        0                0 Jan  3 00:40 ..
--w-------    1 0        0             4096 Jan  3 00:43 export
lrwxrwxrwx    1 0        0                0 Jan  3 00:42 gpio510 -> ../../devices/platform/10000000.sysreg/gpiochip0/gpio/gpio510
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip499 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.3.auto/gpio/gpiochip499
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip500 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.2.auto/gpio/gpiochip500
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip502 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.1.auto/gpio/gpiochip502
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip510 -> ../../devices/platform/10000000.sysreg/gpio/gpiochip510
--w-------    1 0        0             4096 Jan  3 00:43 unexport

/ # grep "" /sys/class/gpio/gpio510/*
/sys/class/gpio/gpio510/active_low:0
/sys/class/gpio/gpio510/direction:in
/sys/class/gpio/gpio510/value:0

/ # echo out > /sys/class/gpio/gpio510/direction
/ # grep "" /sys/class/gpio/gpio510/*
/sys/class/gpio/gpio510/active_low:0
/sys/class/gpio/gpio510/direction:out
/sys/class/gpio/gpio510/value:0

/ # cat /sys/kernel/debug/gpio
gpiochip3: GPIOs 499-499, parent: platform/basic-mmio-gpio.3.auto, sys_flash:

gpiochip2: GPIOs 500-501, parent: platform/basic-mmio-gpio.2.auto, sys_mci:
 gpio-500 (                    |cd                  ) in  lo
 gpio-501 (                    |wp                  ) in  lo

gpiochip1: GPIOs 502-509, parent: platform/basic-mmio-gpio.1.auto, sys_led:
 gpio-502 (                    |?                   ) out lo
 gpio-503 (                    |?                   ) out lo
 gpio-504 (                    |?                   ) out lo
 gpio-505 (                    |?                   ) out lo
 gpio-506 (                    |?                   ) out hi
 gpio-507 (                    |?                   ) out hi
 gpio-508 (                    |?                   ) out lo
 gpio-509 (                    |?                   ) out lo

gpiochip0: GPIOs 510-511, parent: platform/10000000.sysreg, 10000000.sysreg:
 gpio-510 (                    |sysfs               ) out lo

/ # grep "" /sys/class/gpio/gpiochip*/*
/sys/class/gpio/gpiochip499/base:499
/sys/class/gpio/gpiochip499/label:sys_flash
/sys/class/gpio/gpiochip499/ngpio:1
/sys/class/gpio/gpiochip500/base:500
/sys/class/gpio/gpiochip500/label:sys_mci
/sys/class/gpio/gpiochip500/ngpio:2
/sys/class/gpio/gpiochip502/base:502
/sys/class/gpio/gpiochip502/label:sys_led
/sys/class/gpio/gpiochip502/ngpio:8
/sys/class/gpio/gpiochip510/base:510
/sys/class/gpio/gpiochip510/label:10000000.sysreg
/sys/class/gpio/gpiochip510/ngpio:2



2022年8月14日 星期日

RFC 1319 - MD2 Message-Digest Algorithm


Message Digest 演算法是以隨意長度的message作為input, 並產生128-bit的"fingerprint"或"message "digest". 雖然RFC後面有附上實作, 但是網路上這篇比較容易閱讀, 有助於理解MD2的演算法,
/**
 * @file md2.c
 * @author .ukasz Grudnik (https://github.com/lukaszgrudnik)
 * @brief MD2 algorithm based on RFC documentation https://datatracker.ietf.org/doc/html/rfc1319
 * @version 0.1
 * @date 2022-06-26
 *
 * @copyright Copyright (c) 2022
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// This step uses a 256-byte "random" permutation constructed from the
//    digits of pi. Let S[i] denote the i-th element of this table.
static unsigned char S[256] = {
    41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
    19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
    76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
    138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
    245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
    148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
    39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
    181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
    150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
    112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
    96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
    85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
    234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
    129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
    8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
    203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
    166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
    31, 26, 219, 153, 141, 51, 159, 17, 131, 20
};

int main(int argc, char *argv[])
{

    if (argc == 1) {
        printf("error: Type message to hash\n");
        printf("./a.out <message>\n");
        return -1;
    }

    // Step 1. Append Padding Bytes
    int p, N, n;


    // The message is "padded" (extended) so that its length (in bytes) is
    //   congruent to 0, modulo 16.
    n = strlen(argv[1]) * sizeof(unsigned char);
    p = 16 - n % 16;
    N = n + p;

    unsigned char *M = (unsigned char *)malloc(N);

    // Padding is performed as follows: "i" bytes of value "i" are appended
    //   to the message so that the length in bytes of the padded message
    //   becomes congruent to 0, modulo 16. At least one byte and at most 16
    //   16 bytes are appended.
    memcpy(M, argv[1], n);
    memset(M+n, p, p);

    // Step 2. Append Checksum
    // Clear checksum.
    unsigned char C[16];
    memset(C, 0, 16);

    unsigned char c;
    // Set L to 0.
    unsigned char L = 0;

    // Process each 16-word block.
    for (int i = 0; i <= N / 16 - 1; i++) {
        for (int j = 0; j <= 15; j++) {
            c = M[i * 16 + j]; // Set c to M[i*16+j]
            C[j] ^= S[c ^ L];  // Set C[j] to S[c xor L]
            L = C[j];          // Set L to C[j]
        }
    }

    // The 16-byte checksum C[0 ... 15] is appended to the message
    // Let M[0] with checksum), where N' = N + 16.
    int N_ = N + 16;
    unsigned char *M_ = (unsigned char *)malloc(N_);

    memcpy(M_, M, N);
    memcpy(M_ + N, C, 16);

    //  Step 3. Initialize MD Buffer
    //  A 48-byte buffer X is used to compute the message digest.
    //  The buffer is initialized to zero.
    unsigned char X[48] = {0};

    //  Step 4. Process Message in 16-Byte Blocks
    unsigned int t = 0;

    for (int m = 0; m < 16; m++) {
        printf("%d ", M_[N + m]);
    }

    // Process each 16-word block
    for (int i = 0; i <= (N_ / 16) - 1; i++) {
        // Copy block i into X.
        for (int j = 0; j <= 15; j++) {
            X[16 + j] = M_[16 * i + j]; // Set X[16+j] to M[i*16+j].
            X[32 + j] = (X[16 + j] ^ X[j]); // Set X[32+j] to (X[16+j] xor X[j])
        }

        // Set t to 0.
        t = 0;
        // Do 18 rounds.
        for (int j = 0; j <= 17; j++) {
            for (int k = 0; k <= 47; k++) {
                t = X[k] ^= S[t]; // Set t and X[k] to (X[k] xor S[t]).
            }
            t = (t + j) % 256; // Set t to (t+j) modulo 256.
        }
    }

    // Step 5. Output
    printf("\nHash:\n");
    for (int i = 0; i < 48; i++) {
        printf("%02x ", X[i]);
    }
    return 0;
}

    參考資料:
  • https://www.ietf.org/rfc/rfc1319.txt
  • https://github.com/lukaszgrudnik/MD2
  • https://nickthecrypt.medium.com/cryptography-hash-method-md2-message-digest-2-step-by-step-explanation-made-easy-with-python-10faa2e35e85




熱門文章