From 891595b9307eb2695411d8e32e4addd9cd927ec8 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sun, 12 May 2024 15:21:19 +0200 Subject: rename folders --- 1/Makefile | 21 -------- 1/config.h | 26 --------- 1/fopdrv.c | 76 -------------------------- 1/fopdrv.h | 11 ---- 1/main.c | 110 ------------------------------------- 1/test/.gitignore | 1 - 1/test/main.c | 56 ------------------- 1/test/makefile | 2 - 1/test/test.sh | 24 --------- 2/.gitignore | 3 -- 2/Containerfile | 13 ----- 2/makefile | 36 ------------- 2/module/Makefile | 12 ----- 2/module/config.h | 1 - 2/module/fopdrv.c | 1 - 2/module/fopdrv.h | 1 - 2/module/main.c | 1 - 2/readme.md | 18 ------- 2/rice.sh | 132 --------------------------------------------- 2/test/.gitignore | 1 - 2/test/main.c | 6 --- 2/test/makefile | 1 - beaglebone/.gitignore | 3 ++ beaglebone/Containerfile | 13 +++++ beaglebone/makefile | 36 +++++++++++++ beaglebone/module/Makefile | 12 +++++ beaglebone/module/config.h | 1 + beaglebone/module/fopdrv.c | 1 + beaglebone/module/fopdrv.h | 1 + beaglebone/module/main.c | 1 + beaglebone/readme.md | 18 +++++++ beaglebone/rice.sh | 132 +++++++++++++++++++++++++++++++++++++++++++++ beaglebone/test/.gitignore | 1 + beaglebone/test/main.c | 6 +++ beaglebone/test/makefile | 1 + driver/Makefile | 21 ++++++++ driver/config.h | 26 +++++++++ driver/fopdrv.c | 76 ++++++++++++++++++++++++++ driver/fopdrv.h | 11 ++++ driver/main.c | 110 +++++++++++++++++++++++++++++++++++++ driver/test/.gitignore | 1 + driver/test/main.c | 56 +++++++++++++++++++ driver/test/makefile | 2 + driver/test/test.sh | 24 +++++++++ 44 files changed, 553 insertions(+), 553 deletions(-) delete mode 100644 1/Makefile delete mode 100644 1/config.h delete mode 100644 1/fopdrv.c delete mode 100644 1/fopdrv.h delete mode 100644 1/main.c delete mode 100644 1/test/.gitignore delete mode 100644 1/test/main.c delete mode 100644 1/test/makefile delete mode 100755 1/test/test.sh delete mode 100644 2/.gitignore delete mode 100644 2/Containerfile delete mode 100644 2/makefile delete mode 100644 2/module/Makefile delete mode 120000 2/module/config.h delete mode 120000 2/module/fopdrv.c delete mode 120000 2/module/fopdrv.h delete mode 120000 2/module/main.c delete mode 100644 2/readme.md delete mode 100755 2/rice.sh delete mode 100644 2/test/.gitignore delete mode 100644 2/test/main.c delete mode 100644 2/test/makefile create mode 100644 beaglebone/.gitignore create mode 100644 beaglebone/Containerfile create mode 100644 beaglebone/makefile create mode 100644 beaglebone/module/Makefile create mode 120000 beaglebone/module/config.h create mode 120000 beaglebone/module/fopdrv.c create mode 120000 beaglebone/module/fopdrv.h create mode 120000 beaglebone/module/main.c create mode 100644 beaglebone/readme.md create mode 100755 beaglebone/rice.sh create mode 100644 beaglebone/test/.gitignore create mode 100644 beaglebone/test/main.c create mode 100644 beaglebone/test/makefile create mode 100644 driver/Makefile create mode 100644 driver/config.h create mode 100644 driver/fopdrv.c create mode 100644 driver/fopdrv.h create mode 100644 driver/main.c create mode 100644 driver/test/.gitignore create mode 100644 driver/test/main.c create mode 100644 driver/test/makefile create mode 100755 driver/test/test.sh diff --git a/1/Makefile b/1/Makefile deleted file mode 100644 index d968e31..0000000 --- a/1/Makefile +++ /dev/null @@ -1,21 +0,0 @@ -RELEASE := $(shell uname -r) -KERNEL := /lib/modules/$(RELEASE)/build -# PWD doesn't have to be defined as it's inherited from the current environment -# MAKE shouldn't be defined as it's special in GNU make - -# the names of these variables are required by the kernel's (imo weird) build -# system, see [kbuild-obj-var] and [kbuild-module-makefile] in ../readme.md -obj-m += lork.o -lork-y += main.o -lork-y += fopdrv.o - -all: - $(MAKE) -C $(KERNEL) M=$(PWD) modules - -# the generated compile_commands.json needs to be edited to point to the kernel -# folder as working directory because the include paths are relative -compile_commands.json: - compiledb -o $@.tmp make -Bn - jq --arg dir $(KERNEL) '.[].directory = $$dir' $@.tmp > $@ - $(RM) $@.tmp - diff --git a/1/config.h b/1/config.h deleted file mode 100644 index dbe3a86..0000000 --- a/1/config.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#define DRV_NAME "lork" - -#include - -// values from - -typedef uint32_t ti_am335x_word_t; - -// p. 180: "Control Module" -#define TI_AM335X_CM_ADDR 0x44E10000 -// p. 182: "GPIO1" -#define TI_AM335X_GPIO1_ADDR 0x4804C000 -// p. 1459: "conf_gpmc_a3" -#define TI_AM335X_CM_GPMC_A3_ADDR (TI_AM335X_CM_ADDR + 0x84C) - -// p. 4990, variable names copied as-is -#define GPIO_OE (0x134 / (sizeof(ti_am335x_word_t))) -#define GPIO_DATAIN (0x138 / (sizeof(ti_am335x_word_t))) -#define GPIO_CLEARDATAOUT (0x190 / (sizeof(ti_am335x_word_t))) -#define GPIO_SETDATAOUT (0x194 / (sizeof(ti_am335x_word_t))) -// total size of GPIO registers from p. 4990 (in bytes) -#define GPIO_REG_SIZE 0x198 - -#define PIN 19 diff --git a/1/fopdrv.c b/1/fopdrv.c deleted file mode 100644 index 18a1d35..0000000 --- a/1/fopdrv.c +++ /dev/null @@ -1,76 +0,0 @@ -#include - -#include "fopdrv.h" -#include "config.h" - -bool printed = false; - -// driver/char/mem.c read_null (/dev/null) -ssize_t fop_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { - printk("%s(, , %u, )\n", __PRETTY_FUNCTION__, count); - - if (count < 2) return 0; - if (printed) return 0; - - ti_am335x_word_t* gpio1 = ioremap(TI_AM335X_GPIO1_ADDR, GPIO_REG_SIZE); - barrier(); - - ti_am335x_word_t gpio = ioread32(gpio1 + GPIO_DATAIN); rmb(); - bool on = (gpio & (1< 0; - - char output[10]; - snprintf(output, 10, "%d\n", on); - - if (copy_to_user(buf, output + *ppos, 2)) - return -EFAULT; - *ppos += 2; - printed = true; - return 2; -} - -ssize_t fop_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { - printk("%s(, , %u, )\n", __PRETTY_FUNCTION__, count); - - // only allow single character as input - if (count < 1) return count; - if (count > 2) return count; - - // copy buffer for reading (see [kernel-labs-chrdev] in ../readme.md) - char input_buf[10]; - if (copy_from_user(input_buf + *ppos, buf, count)) - return -EFAULT; - - ti_am335x_word_t* gpio1 = ioremap(TI_AM335X_GPIO1_ADDR, GPIO_REG_SIZE); - barrier(); - - if (input_buf[0] == '0') { - printk("TODO: TURN OFF OUTPUT\n"); - iowrite32((1<, )\n", __PRETTY_FUNCTION__); - printed = false; - return 0; - // 0 seems to be a safe return value as it's used in driver/char/mem.c. The - // manual page for open(2) says that the system call returns a nonnegative - // integer representing the file descriptor on success, but it does not - // appears to be required. -} - -int fop_release(struct inode * inode, struct file * file) { - printk("%s(, )\n", __PRETTY_FUNCTION__); - return 0; - // same as above, but found in driver/char/lp.c -} - - diff --git a/1/fopdrv.h b/1/fopdrv.h deleted file mode 100644 index f2ca18c..0000000 --- a/1/fopdrv.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include -#include - -// declarations copied from linux/fs.h -ssize_t fop_read(struct file *, char __user *, size_t, loff_t *); -ssize_t fop_write(struct file *, const char __user *, size_t, loff_t *); -int fop_open(struct inode *, struct file *); -int fop_release(struct inode *, struct file *); - diff --git a/1/main.c b/1/main.c deleted file mode 100644 index d8ea7fc..0000000 --- a/1/main.c +++ /dev/null @@ -1,110 +0,0 @@ -#include -#include -#include -#include - -#include "fopdrv.h" -#include "config.h" - -int one = 0; -module_param(one, int, 0400); -MODULE_PARM_DESC(one, "first parameter, only printed on init"); - -int not_named_two = 0; -module_param_named(two, not_named_two, int, 0400); -MODULE_PARM_DESC(not_named_two, "second parameter, only printed on init"); - -struct cdev *cdev; -struct device *dev; -struct class *class; -dev_t node; -struct file_operations fops = { - .read = fop_read, - .write = fop_write, - .open = fop_open, - .release = fop_release, -}; - -static int mod_init(void) { - int err; - - err = alloc_chrdev_region(&node, 0, 1, DRV_NAME); - if (err < 0) { - printk(KERN_ERR "alloc_chrdev_region error %d\n", err); - goto return_err; - } - - class = class_create(DRV_NAME); - if (IS_ERR_OR_NULL(class)) { - err = PTR_ERR(class); - printk(KERN_ERR "class_create error %d\n", err); - goto free_class; - } - - cdev = cdev_alloc(); - if (!cdev) { - err = ENOMEM; - goto free_cdev; - } - - cdev->ops = &fops; - cdev_init(cdev, &fops); - err = cdev_add(cdev, node, 1); - if (err < 0) { - printk(KERN_ERR "cdev_add error %d\n", err); - goto free_cdev; - } - - dev = device_create(class, NULL, node, NULL, DRV_NAME); - if (IS_ERR_OR_NULL(dev)) { - err = PTR_ERR(dev); - printk(KERN_ERR "device_create error %d\n", err); - goto free_device; - } - - printk("%s() -> 0 (%d:%d)\n", __PRETTY_FUNCTION__, MAJOR(node), MINOR(node)); - printk("one = %d, two = %d\n", one, not_named_two); - - // configure gpio mux - uint32_t* conf_gpmc_a3 = ioremap(TI_AM335X_CM_GPMC_A3_ADDR, sizeof(ti_am335x_word_t)); - if (conf_gpmc_a3 == NULL) - return -EINVAL; - barrier(); - uint32_t val = ioread32(conf_gpmc_a3); rmb(); - val &= ~0x7; - val |= 0x7; - iowrite32(val, conf_gpmc_a3); wmb(); - iounmap(conf_gpmc_a3); - - // configure output pin - ti_am335x_word_t* gpio1 = ioremap(TI_AM335X_GPIO1_ADDR, GPIO_REG_SIZE); - barrier(); - ti_am335x_word_t oe_reg = ioread32(gpio1 + GPIO_OE); rmb(); - oe_reg &= ~(1< %d\n", __PRETTY_FUNCTION__, err); - return err; -} - -static void mod_exit(void) { - device_destroy(class, node); - cdev_del(cdev); - class_destroy(class); - printk("%s()\n", __PRETTY_FUNCTION__); -} - -module_init(mod_init); -module_exit(mod_exit); -MODULE_LICENSE("Dual MIT/GPL"); - diff --git a/1/test/.gitignore b/1/test/.gitignore deleted file mode 100644 index ba2906d..0000000 --- a/1/test/.gitignore +++ /dev/null @@ -1 +0,0 @@ -main diff --git a/1/test/main.c b/1/test/main.c deleted file mode 100644 index b87e56c..0000000 --- a/1/test/main.c +++ /dev/null @@ -1,56 +0,0 @@ -#include -#include -#include -#include -#include - -int main(int argc, char** argv) { - if (geteuid() != 0) { - fprintf(stderr, "run me as root!\n"); - return 1; - } - - argc--; // argv[0] is the program name - if (argc == 0) { - fprintf(stderr, "usage: %s /dev/lork\n", argv[0]); - return 1; - } - - char* dev = argv[1]; - const char* input = "test"; - int fd; - ssize_t bytes; - - fd = open(dev, O_RDWR); - if (-1 == fd) { - fprintf(stderr, "open() failed with code %d\n", errno); - return 1; - } - - bytes = write(fd, input, strlen(input)); - if (-1 == bytes) { - fprintf(stderr, "write() failed with code %d\n", errno); - return 1; - } - fprintf(stderr, "input \"%s\" to %s (%ld bytes succesful)\n", input, dev, bytes); - - char buf[80]; - bytes = read(fd, buf, 80); - if (-1 == bytes) { - fprintf(stderr, "read() failed with code %d\n", errno); - return 1; - } - fprintf(stderr, "output from %s: (%ld bytes)\n", dev, bytes); - if (bytes > 0) { - if (bytes < 80) buf[bytes] = '\0'; // null terminate string for printf - printf("%s\n", buf); - } - - if (-1 == close(fd)) { - fprintf(stderr, "close() failed with code %d\n", errno); - return 1; - } - - return 0; -} - diff --git a/1/test/makefile b/1/test/makefile deleted file mode 100644 index 3491973..0000000 --- a/1/test/makefile +++ /dev/null @@ -1,2 +0,0 @@ -main: main.o - diff --git a/1/test/test.sh b/1/test/test.sh deleted file mode 100755 index 7dba005..0000000 --- a/1/test/test.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/sh - -if [ $(id -u) -ne 0 ] ; then - echo "run me as root!" >&2 - exit 1 -fi - -if [ $# -eq 0 ] ; then - echo "usage: $0 /dev/lork" >&2 - exit 1 -fi - -DEV="$1" -INPUT="test" - -echo "input \"$INPUT\" to $DEV" >&2 -echo "$INPUT" > "$DEV" -echo "(EC $?)" >&2 - -echo "output from $DEV:" >&2 -cat "$DEV" -echo "(EC $?)" >&2 - -exit 0 diff --git a/2/.gitignore b/2/.gitignore deleted file mode 100644 index 5b74d26..0000000 --- a/2/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -*.deb -*.tar.xz -headers diff --git a/2/Containerfile b/2/Containerfile deleted file mode 100644 index cc368d8..0000000 --- a/2/Containerfile +++ /dev/null @@ -1,13 +0,0 @@ -from docker.io/library/debian:sid - -run apt-get update - -# packages (in separate steps to optimize caching) -run apt-get install -y gcc-arm-linux-gnueabi -run apt-get install -y gcc -run apt-get install -y make -run apt-get install -y git - -# container directory on which the working directory on host is mounted -workdir /workdir - diff --git a/2/makefile b/2/makefile deleted file mode 100644 index 06c2552..0000000 --- a/2/makefile +++ /dev/null @@ -1,36 +0,0 @@ -# see ./rice.sh -RELEASE = 6.6.15-ti-rt-arm32-r1 -DEB_URL = https://repos.rcn-ee.com/debian/pool/main/l/linux-upstream/linux-headers-$(RELEASE)_1sid_armhf.deb - -.PHONY: FORCE - -kernel-headers.deb: - curl -sLo- $(DEB_URL) > $@ - -data.tar.xz: kernel-headers.deb - ar x $< $@ - -headers: data.tar.xz - mkdir -p $@ - tar xf $< -C $@ --strip-components 4 ./usr/src/linux-headers-$(RELEASE) - -CTR := podman -# CTR := sudo podman # rootless not set up or unsupported -CTR_IMG_TAG := avans-drvo - -CTRIZE := $(CTR) run -CTRIZE += --rm -CTRIZE += --tty -CTRIZE += --volume /tmp:/tmp:rw -CTRIZE += --volume .:/workdir:rw -# CTRIZE += --env-file ./env -CTRIZE += --env MAKE* -CTRIZE += $(CTR_IMG_TAG) -export - -container: FORCE - $(CTR) build --tag $(CTR_IMG_TAG) . - -build_test: FORCE - $(CTRIZE) $(MAKE) -C module - diff --git a/2/module/Makefile b/2/module/Makefile deleted file mode 100644 index 01b2177..0000000 --- a/2/module/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -RELEASE = 6.6.15-ti-rt-arm32-r1 -KERNEL := ../headers/ -ARCH := arm -CROSS_COMPILE := arm-linux-gnueabi- - -obj-m += lork.o -lork-y += main.o -lork-y += fopdrv.o - -all: - $(MAKE) -C $(KERNEL) M=$(PWD) modules - diff --git a/2/module/config.h b/2/module/config.h deleted file mode 120000 index bad07c3..0000000 --- a/2/module/config.h +++ /dev/null @@ -1 +0,0 @@ -../../1/config.h \ No newline at end of file diff --git a/2/module/fopdrv.c b/2/module/fopdrv.c deleted file mode 120000 index cbe1e0f..0000000 --- a/2/module/fopdrv.c +++ /dev/null @@ -1 +0,0 @@ -../../1/fopdrv.c \ No newline at end of file diff --git a/2/module/fopdrv.h b/2/module/fopdrv.h deleted file mode 120000 index f9b9e9f..0000000 --- a/2/module/fopdrv.h +++ /dev/null @@ -1 +0,0 @@ -../../1/fopdrv.h \ No newline at end of file diff --git a/2/module/main.c b/2/module/main.c deleted file mode 120000 index 6180bb4..0000000 --- a/2/module/main.c +++ /dev/null @@ -1 +0,0 @@ -../../1/main.c \ No newline at end of file diff --git a/2/readme.md b/2/readme.md deleted file mode 100644 index 5231d8f..0000000 --- a/2/readme.md +++ /dev/null @@ -1,18 +0,0 @@ -I used [this][img] image as a base, and riced the shit out of it using the -steps recorded in [rice.sh](./rice.sh). This script can be run from the above -image by doing the following: - -``` - (login as `debian` with password `temppwd`) - -$ curl -sLo- https://git.pipeframe.xyz/school/drvo/plain/2/rice.sh | sudo sh -s -``` - -This will upgrade the kernel to version `6.6.15-ti-rt-arm32-r1`, which supports -some fancy new features like the new `class_create` method introduced in 6.4. I -actually recommend you *do not do this*, and instead just start writing a -kernel module targeting version 5.10 from week 1, as this kernel version is way -more common. - -[img]: https://www.beagleboard.org/distros/am335x-11-7-2023-09-02-4gb-emmc-xfce-flasher - diff --git a/2/rice.sh b/2/rice.sh deleted file mode 100755 index ea1e7d2..0000000 --- a/2/rice.sh +++ /dev/null @@ -1,132 +0,0 @@ -#!/bin/sh -if [ $(id -u) -ne 0 ] ; then - echo 'this script should be run as root instead!' - exit 1 -fi - -USER_NAME='loek' -HOST_NAME='drvo' -KERNEL_VERSION='6.6.15-ti-rt-arm32-r1' # latest i could find for ti - -# fail ok -fok() { "$@" 1> /dev/null 2> /dev/null ; } - -echo "setting hostname to $HOST_NAME..." -hostname $HOST_NAME -hostname > /etc/hostname -sed -i 's/\/'"$HOST_NAME"'/g' /etc/hosts - -echo "creating user $USER_NAME..." -fok userdel debian -rm -rf /home/debian -fok id -u $USER_NAME || fok adduser $USER_NAME - -echo "removing all security..." -rm -f /etc/sudoers.d/admin -cat << "EOF" > /etc/sudoers.d/nopasswd -%sudo ALL=(ALL:ALL) NOPASSWD: ALL -EOF -usermod -aG sudo $USER_NAME -cat << "EOF" > /etc/ssh/sshd_config.d/rice.conf -PasswordAuthentication yes -PermitEmptyPasswords yes -PermitRootLogin yes -PrintLastLog no -EOF -systemctl restart ssh -fok passwd -d root -fok passwd -d $USER_NAME - -echo "fixing terminal colors & default editor..." -cat << "EOF" > /etc/profile.d/rice.sh -export TERM="xterm-256color" -export EDITOR="nvim" -EOF - -echo "removing bloatware..." -fok apt remove -y --allow-change-held-packages \ - libruby2.7 libgl1-mesa-dri python3-numpy mesa-vulkan-drivers \ - bb-beagle-config libflite1 firmware-libertas python3-mypy libgtk-3-common \ - adwaita-icon-theme firmware-atheros firmware-misc-nonfree \ - pocketsphinx-en-us scratch libwebkit2gtk-4.0-37 firmware-iwlwifi \ - bb-node-red-installer firefox-esr bb-code-server plymouth xserver-xorg-core \ - xserver-xorg xserver-xorg-legacy x11-common x11-server-utils x11-utils \ - lightdm-gtk-greeter lightdm xfce4-session xfce4-panel xfce4-settings \ - xfce4-helpers fonts-dejavu-core nodejs nodejs-doc nginx-core nginx-common \ - nginx mesa-va-drivers bluetooth bluez pipewire-bin pipewire pulseaudio-utils \ - pulseaudio gtk-update-icon-cache wpasupplicant fontconfig-config fontconfig \ - hostapd adwaita-icon-theme hicolor-icon-theme polkitd whiptail -echo "removing more bloatware..." -fok apt autoremove -y -echo "cleaning up trash..." -fok apt clean -y - -echo "updating system..." -fok apt-get update -fok apt-get upgrade -y - -echo "preparing for debian sid update..." -export DEBIAN_FRONTEND=noninteractive -echo "usrmerge usrmerge/autoconvert boolean true" | debconf-set-selections -fok apt-get install usrmerge -y -cat << "EOF" > /etc/apt/sources.list -deb http://deb.debian.org/debian sid main contrib non-free -deb [arch=armhf signed-by=/usr/share/keyrings/rcn-ee-archive-keyring.gpg] http://repos.rcn-ee.com/debian/ sid main -EOF -fok apt-get update -echo "updating to debian sid..." -apt-get dist-upgrade -y - -echo "removing noisy banners..." -rm -f /etc/motd /etc/issue /etc/issue.net /etc/dogtag - -echo "install newer linux kernel..." -fok apt-get install -y linux-image-$KERNEL_VERSION linux-headers-$KERNEL_VERSION - -echo "install based software..." -fok apt-get install -y neovim zsh git systemd-resolved -fok chsh -s /bin/zsh $USER_NAME -fok chsh -s /bin/zsh root - -cp /etc/zsh/newuser.zshrc.recommended "/home/$USER_NAME/.zshrc" -chown "$USER_NAME:$USER_NAME" "/home/$USER_NAME/.zshrc" -cat << "EOF" > /root/.zshrc -# Set up the prompt -autoload -Uz promptinit -promptinit -prompt redhat - -setopt histignorealldups sharehistory - -# Use emacs keybindings even if our EDITOR is set to vi -bindkey -e - -# Keep 1000 lines of history within the shell and save it to ~/.zsh_history: -HISTSIZE=1000 -SAVEHIST=1000 -HISTFILE=~/.zsh_history - -# Use modern completion system -autoload -Uz compinit -compinit - -zstyle ':completion:*' auto-description 'specify: %d' -zstyle ':completion:*' completer _expand _complete _correct _approximate -zstyle ':completion:*' format 'Completing %d' -zstyle ':completion:*' group-name '' -zstyle ':completion:*' menu select=2 -zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s -zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=* l:|=*' -zstyle ':completion:*' menu select=long -zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s -zstyle ':completion:*' use-compctl false -zstyle ':completion:*' verbose true - -zstyle ':completion:*:kill:*' command 'ps -u $USER -o pid,%cpu,tty,cputime,cmd' -EOF -echo "emulate sh -c 'source /etc/profile'" >> /etc/zsh/zprofile - -echo "cleaning up trash..." -fok apt clean -y - -echo "all done!" diff --git a/2/test/.gitignore b/2/test/.gitignore deleted file mode 100644 index ba2906d..0000000 --- a/2/test/.gitignore +++ /dev/null @@ -1 +0,0 @@ -main diff --git a/2/test/main.c b/2/test/main.c deleted file mode 100644 index 04ce434..0000000 --- a/2/test/main.c +++ /dev/null @@ -1,6 +0,0 @@ -#include - -int main() { - printf("Hello world!\n"); - return 0; -} diff --git a/2/test/makefile b/2/test/makefile deleted file mode 100644 index cd6a8c6..0000000 --- a/2/test/makefile +++ /dev/null @@ -1 +0,0 @@ -main: main.c diff --git a/beaglebone/.gitignore b/beaglebone/.gitignore new file mode 100644 index 0000000..5b74d26 --- /dev/null +++ b/beaglebone/.gitignore @@ -0,0 +1,3 @@ +*.deb +*.tar.xz +headers diff --git a/beaglebone/Containerfile b/beaglebone/Containerfile new file mode 100644 index 0000000..cc368d8 --- /dev/null +++ b/beaglebone/Containerfile @@ -0,0 +1,13 @@ +from docker.io/library/debian:sid + +run apt-get update + +# packages (in separate steps to optimize caching) +run apt-get install -y gcc-arm-linux-gnueabi +run apt-get install -y gcc +run apt-get install -y make +run apt-get install -y git + +# container directory on which the working directory on host is mounted +workdir /workdir + diff --git a/beaglebone/makefile b/beaglebone/makefile new file mode 100644 index 0000000..06c2552 --- /dev/null +++ b/beaglebone/makefile @@ -0,0 +1,36 @@ +# see ./rice.sh +RELEASE = 6.6.15-ti-rt-arm32-r1 +DEB_URL = https://repos.rcn-ee.com/debian/pool/main/l/linux-upstream/linux-headers-$(RELEASE)_1sid_armhf.deb + +.PHONY: FORCE + +kernel-headers.deb: + curl -sLo- $(DEB_URL) > $@ + +data.tar.xz: kernel-headers.deb + ar x $< $@ + +headers: data.tar.xz + mkdir -p $@ + tar xf $< -C $@ --strip-components 4 ./usr/src/linux-headers-$(RELEASE) + +CTR := podman +# CTR := sudo podman # rootless not set up or unsupported +CTR_IMG_TAG := avans-drvo + +CTRIZE := $(CTR) run +CTRIZE += --rm +CTRIZE += --tty +CTRIZE += --volume /tmp:/tmp:rw +CTRIZE += --volume .:/workdir:rw +# CTRIZE += --env-file ./env +CTRIZE += --env MAKE* +CTRIZE += $(CTR_IMG_TAG) +export + +container: FORCE + $(CTR) build --tag $(CTR_IMG_TAG) . + +build_test: FORCE + $(CTRIZE) $(MAKE) -C module + diff --git a/beaglebone/module/Makefile b/beaglebone/module/Makefile new file mode 100644 index 0000000..01b2177 --- /dev/null +++ b/beaglebone/module/Makefile @@ -0,0 +1,12 @@ +RELEASE = 6.6.15-ti-rt-arm32-r1 +KERNEL := ../headers/ +ARCH := arm +CROSS_COMPILE := arm-linux-gnueabi- + +obj-m += lork.o +lork-y += main.o +lork-y += fopdrv.o + +all: + $(MAKE) -C $(KERNEL) M=$(PWD) modules + diff --git a/beaglebone/module/config.h b/beaglebone/module/config.h new file mode 120000 index 0000000..bad07c3 --- /dev/null +++ b/beaglebone/module/config.h @@ -0,0 +1 @@ +../../1/config.h \ No newline at end of file diff --git a/beaglebone/module/fopdrv.c b/beaglebone/module/fopdrv.c new file mode 120000 index 0000000..cbe1e0f --- /dev/null +++ b/beaglebone/module/fopdrv.c @@ -0,0 +1 @@ +../../1/fopdrv.c \ No newline at end of file diff --git a/beaglebone/module/fopdrv.h b/beaglebone/module/fopdrv.h new file mode 120000 index 0000000..f9b9e9f --- /dev/null +++ b/beaglebone/module/fopdrv.h @@ -0,0 +1 @@ +../../1/fopdrv.h \ No newline at end of file diff --git a/beaglebone/module/main.c b/beaglebone/module/main.c new file mode 120000 index 0000000..6180bb4 --- /dev/null +++ b/beaglebone/module/main.c @@ -0,0 +1 @@ +../../1/main.c \ No newline at end of file diff --git a/beaglebone/readme.md b/beaglebone/readme.md new file mode 100644 index 0000000..5231d8f --- /dev/null +++ b/beaglebone/readme.md @@ -0,0 +1,18 @@ +I used [this][img] image as a base, and riced the shit out of it using the +steps recorded in [rice.sh](./rice.sh). This script can be run from the above +image by doing the following: + +``` + (login as `debian` with password `temppwd`) + +$ curl -sLo- https://git.pipeframe.xyz/school/drvo/plain/2/rice.sh | sudo sh -s +``` + +This will upgrade the kernel to version `6.6.15-ti-rt-arm32-r1`, which supports +some fancy new features like the new `class_create` method introduced in 6.4. I +actually recommend you *do not do this*, and instead just start writing a +kernel module targeting version 5.10 from week 1, as this kernel version is way +more common. + +[img]: https://www.beagleboard.org/distros/am335x-11-7-2023-09-02-4gb-emmc-xfce-flasher + diff --git a/beaglebone/rice.sh b/beaglebone/rice.sh new file mode 100755 index 0000000..ea1e7d2 --- /dev/null +++ b/beaglebone/rice.sh @@ -0,0 +1,132 @@ +#!/bin/sh +if [ $(id -u) -ne 0 ] ; then + echo 'this script should be run as root instead!' + exit 1 +fi + +USER_NAME='loek' +HOST_NAME='drvo' +KERNEL_VERSION='6.6.15-ti-rt-arm32-r1' # latest i could find for ti + +# fail ok +fok() { "$@" 1> /dev/null 2> /dev/null ; } + +echo "setting hostname to $HOST_NAME..." +hostname $HOST_NAME +hostname > /etc/hostname +sed -i 's/\/'"$HOST_NAME"'/g' /etc/hosts + +echo "creating user $USER_NAME..." +fok userdel debian +rm -rf /home/debian +fok id -u $USER_NAME || fok adduser $USER_NAME + +echo "removing all security..." +rm -f /etc/sudoers.d/admin +cat << "EOF" > /etc/sudoers.d/nopasswd +%sudo ALL=(ALL:ALL) NOPASSWD: ALL +EOF +usermod -aG sudo $USER_NAME +cat << "EOF" > /etc/ssh/sshd_config.d/rice.conf +PasswordAuthentication yes +PermitEmptyPasswords yes +PermitRootLogin yes +PrintLastLog no +EOF +systemctl restart ssh +fok passwd -d root +fok passwd -d $USER_NAME + +echo "fixing terminal colors & default editor..." +cat << "EOF" > /etc/profile.d/rice.sh +export TERM="xterm-256color" +export EDITOR="nvim" +EOF + +echo "removing bloatware..." +fok apt remove -y --allow-change-held-packages \ + libruby2.7 libgl1-mesa-dri python3-numpy mesa-vulkan-drivers \ + bb-beagle-config libflite1 firmware-libertas python3-mypy libgtk-3-common \ + adwaita-icon-theme firmware-atheros firmware-misc-nonfree \ + pocketsphinx-en-us scratch libwebkit2gtk-4.0-37 firmware-iwlwifi \ + bb-node-red-installer firefox-esr bb-code-server plymouth xserver-xorg-core \ + xserver-xorg xserver-xorg-legacy x11-common x11-server-utils x11-utils \ + lightdm-gtk-greeter lightdm xfce4-session xfce4-panel xfce4-settings \ + xfce4-helpers fonts-dejavu-core nodejs nodejs-doc nginx-core nginx-common \ + nginx mesa-va-drivers bluetooth bluez pipewire-bin pipewire pulseaudio-utils \ + pulseaudio gtk-update-icon-cache wpasupplicant fontconfig-config fontconfig \ + hostapd adwaita-icon-theme hicolor-icon-theme polkitd whiptail +echo "removing more bloatware..." +fok apt autoremove -y +echo "cleaning up trash..." +fok apt clean -y + +echo "updating system..." +fok apt-get update +fok apt-get upgrade -y + +echo "preparing for debian sid update..." +export DEBIAN_FRONTEND=noninteractive +echo "usrmerge usrmerge/autoconvert boolean true" | debconf-set-selections +fok apt-get install usrmerge -y +cat << "EOF" > /etc/apt/sources.list +deb http://deb.debian.org/debian sid main contrib non-free +deb [arch=armhf signed-by=/usr/share/keyrings/rcn-ee-archive-keyring.gpg] http://repos.rcn-ee.com/debian/ sid main +EOF +fok apt-get update +echo "updating to debian sid..." +apt-get dist-upgrade -y + +echo "removing noisy banners..." +rm -f /etc/motd /etc/issue /etc/issue.net /etc/dogtag + +echo "install newer linux kernel..." +fok apt-get install -y linux-image-$KERNEL_VERSION linux-headers-$KERNEL_VERSION + +echo "install based software..." +fok apt-get install -y neovim zsh git systemd-resolved +fok chsh -s /bin/zsh $USER_NAME +fok chsh -s /bin/zsh root + +cp /etc/zsh/newuser.zshrc.recommended "/home/$USER_NAME/.zshrc" +chown "$USER_NAME:$USER_NAME" "/home/$USER_NAME/.zshrc" +cat << "EOF" > /root/.zshrc +# Set up the prompt +autoload -Uz promptinit +promptinit +prompt redhat + +setopt histignorealldups sharehistory + +# Use emacs keybindings even if our EDITOR is set to vi +bindkey -e + +# Keep 1000 lines of history within the shell and save it to ~/.zsh_history: +HISTSIZE=1000 +SAVEHIST=1000 +HISTFILE=~/.zsh_history + +# Use modern completion system +autoload -Uz compinit +compinit + +zstyle ':completion:*' auto-description 'specify: %d' +zstyle ':completion:*' completer _expand _complete _correct _approximate +zstyle ':completion:*' format 'Completing %d' +zstyle ':completion:*' group-name '' +zstyle ':completion:*' menu select=2 +zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s +zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=* l:|=*' +zstyle ':completion:*' menu select=long +zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s +zstyle ':completion:*' use-compctl false +zstyle ':completion:*' verbose true + +zstyle ':completion:*:kill:*' command 'ps -u $USER -o pid,%cpu,tty,cputime,cmd' +EOF +echo "emulate sh -c 'source /etc/profile'" >> /etc/zsh/zprofile + +echo "cleaning up trash..." +fok apt clean -y + +echo "all done!" diff --git a/beaglebone/test/.gitignore b/beaglebone/test/.gitignore new file mode 100644 index 0000000..ba2906d --- /dev/null +++ b/beaglebone/test/.gitignore @@ -0,0 +1 @@ +main diff --git a/beaglebone/test/main.c b/beaglebone/test/main.c new file mode 100644 index 0000000..04ce434 --- /dev/null +++ b/beaglebone/test/main.c @@ -0,0 +1,6 @@ +#include + +int main() { + printf("Hello world!\n"); + return 0; +} diff --git a/beaglebone/test/makefile b/beaglebone/test/makefile new file mode 100644 index 0000000..cd6a8c6 --- /dev/null +++ b/beaglebone/test/makefile @@ -0,0 +1 @@ +main: main.c diff --git a/driver/Makefile b/driver/Makefile new file mode 100644 index 0000000..d968e31 --- /dev/null +++ b/driver/Makefile @@ -0,0 +1,21 @@ +RELEASE := $(shell uname -r) +KERNEL := /lib/modules/$(RELEASE)/build +# PWD doesn't have to be defined as it's inherited from the current environment +# MAKE shouldn't be defined as it's special in GNU make + +# the names of these variables are required by the kernel's (imo weird) build +# system, see [kbuild-obj-var] and [kbuild-module-makefile] in ../readme.md +obj-m += lork.o +lork-y += main.o +lork-y += fopdrv.o + +all: + $(MAKE) -C $(KERNEL) M=$(PWD) modules + +# the generated compile_commands.json needs to be edited to point to the kernel +# folder as working directory because the include paths are relative +compile_commands.json: + compiledb -o $@.tmp make -Bn + jq --arg dir $(KERNEL) '.[].directory = $$dir' $@.tmp > $@ + $(RM) $@.tmp + diff --git a/driver/config.h b/driver/config.h new file mode 100644 index 0000000..dbe3a86 --- /dev/null +++ b/driver/config.h @@ -0,0 +1,26 @@ +#pragma once + +#define DRV_NAME "lork" + +#include + +// values from + +typedef uint32_t ti_am335x_word_t; + +// p. 180: "Control Module" +#define TI_AM335X_CM_ADDR 0x44E10000 +// p. 182: "GPIO1" +#define TI_AM335X_GPIO1_ADDR 0x4804C000 +// p. 1459: "conf_gpmc_a3" +#define TI_AM335X_CM_GPMC_A3_ADDR (TI_AM335X_CM_ADDR + 0x84C) + +// p. 4990, variable names copied as-is +#define GPIO_OE (0x134 / (sizeof(ti_am335x_word_t))) +#define GPIO_DATAIN (0x138 / (sizeof(ti_am335x_word_t))) +#define GPIO_CLEARDATAOUT (0x190 / (sizeof(ti_am335x_word_t))) +#define GPIO_SETDATAOUT (0x194 / (sizeof(ti_am335x_word_t))) +// total size of GPIO registers from p. 4990 (in bytes) +#define GPIO_REG_SIZE 0x198 + +#define PIN 19 diff --git a/driver/fopdrv.c b/driver/fopdrv.c new file mode 100644 index 0000000..18a1d35 --- /dev/null +++ b/driver/fopdrv.c @@ -0,0 +1,76 @@ +#include + +#include "fopdrv.h" +#include "config.h" + +bool printed = false; + +// driver/char/mem.c read_null (/dev/null) +ssize_t fop_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { + printk("%s(, , %u, )\n", __PRETTY_FUNCTION__, count); + + if (count < 2) return 0; + if (printed) return 0; + + ti_am335x_word_t* gpio1 = ioremap(TI_AM335X_GPIO1_ADDR, GPIO_REG_SIZE); + barrier(); + + ti_am335x_word_t gpio = ioread32(gpio1 + GPIO_DATAIN); rmb(); + bool on = (gpio & (1< 0; + + char output[10]; + snprintf(output, 10, "%d\n", on); + + if (copy_to_user(buf, output + *ppos, 2)) + return -EFAULT; + *ppos += 2; + printed = true; + return 2; +} + +ssize_t fop_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { + printk("%s(, , %u, )\n", __PRETTY_FUNCTION__, count); + + // only allow single character as input + if (count < 1) return count; + if (count > 2) return count; + + // copy buffer for reading (see [kernel-labs-chrdev] in ../readme.md) + char input_buf[10]; + if (copy_from_user(input_buf + *ppos, buf, count)) + return -EFAULT; + + ti_am335x_word_t* gpio1 = ioremap(TI_AM335X_GPIO1_ADDR, GPIO_REG_SIZE); + barrier(); + + if (input_buf[0] == '0') { + printk("TODO: TURN OFF OUTPUT\n"); + iowrite32((1<, )\n", __PRETTY_FUNCTION__); + printed = false; + return 0; + // 0 seems to be a safe return value as it's used in driver/char/mem.c. The + // manual page for open(2) says that the system call returns a nonnegative + // integer representing the file descriptor on success, but it does not + // appears to be required. +} + +int fop_release(struct inode * inode, struct file * file) { + printk("%s(, )\n", __PRETTY_FUNCTION__); + return 0; + // same as above, but found in driver/char/lp.c +} + + diff --git a/driver/fopdrv.h b/driver/fopdrv.h new file mode 100644 index 0000000..f2ca18c --- /dev/null +++ b/driver/fopdrv.h @@ -0,0 +1,11 @@ +#pragma once + +#include +#include + +// declarations copied from linux/fs.h +ssize_t fop_read(struct file *, char __user *, size_t, loff_t *); +ssize_t fop_write(struct file *, const char __user *, size_t, loff_t *); +int fop_open(struct inode *, struct file *); +int fop_release(struct inode *, struct file *); + diff --git a/driver/main.c b/driver/main.c new file mode 100644 index 0000000..d8ea7fc --- /dev/null +++ b/driver/main.c @@ -0,0 +1,110 @@ +#include +#include +#include +#include + +#include "fopdrv.h" +#include "config.h" + +int one = 0; +module_param(one, int, 0400); +MODULE_PARM_DESC(one, "first parameter, only printed on init"); + +int not_named_two = 0; +module_param_named(two, not_named_two, int, 0400); +MODULE_PARM_DESC(not_named_two, "second parameter, only printed on init"); + +struct cdev *cdev; +struct device *dev; +struct class *class; +dev_t node; +struct file_operations fops = { + .read = fop_read, + .write = fop_write, + .open = fop_open, + .release = fop_release, +}; + +static int mod_init(void) { + int err; + + err = alloc_chrdev_region(&node, 0, 1, DRV_NAME); + if (err < 0) { + printk(KERN_ERR "alloc_chrdev_region error %d\n", err); + goto return_err; + } + + class = class_create(DRV_NAME); + if (IS_ERR_OR_NULL(class)) { + err = PTR_ERR(class); + printk(KERN_ERR "class_create error %d\n", err); + goto free_class; + } + + cdev = cdev_alloc(); + if (!cdev) { + err = ENOMEM; + goto free_cdev; + } + + cdev->ops = &fops; + cdev_init(cdev, &fops); + err = cdev_add(cdev, node, 1); + if (err < 0) { + printk(KERN_ERR "cdev_add error %d\n", err); + goto free_cdev; + } + + dev = device_create(class, NULL, node, NULL, DRV_NAME); + if (IS_ERR_OR_NULL(dev)) { + err = PTR_ERR(dev); + printk(KERN_ERR "device_create error %d\n", err); + goto free_device; + } + + printk("%s() -> 0 (%d:%d)\n", __PRETTY_FUNCTION__, MAJOR(node), MINOR(node)); + printk("one = %d, two = %d\n", one, not_named_two); + + // configure gpio mux + uint32_t* conf_gpmc_a3 = ioremap(TI_AM335X_CM_GPMC_A3_ADDR, sizeof(ti_am335x_word_t)); + if (conf_gpmc_a3 == NULL) + return -EINVAL; + barrier(); + uint32_t val = ioread32(conf_gpmc_a3); rmb(); + val &= ~0x7; + val |= 0x7; + iowrite32(val, conf_gpmc_a3); wmb(); + iounmap(conf_gpmc_a3); + + // configure output pin + ti_am335x_word_t* gpio1 = ioremap(TI_AM335X_GPIO1_ADDR, GPIO_REG_SIZE); + barrier(); + ti_am335x_word_t oe_reg = ioread32(gpio1 + GPIO_OE); rmb(); + oe_reg &= ~(1< %d\n", __PRETTY_FUNCTION__, err); + return err; +} + +static void mod_exit(void) { + device_destroy(class, node); + cdev_del(cdev); + class_destroy(class); + printk("%s()\n", __PRETTY_FUNCTION__); +} + +module_init(mod_init); +module_exit(mod_exit); +MODULE_LICENSE("Dual MIT/GPL"); + diff --git a/driver/test/.gitignore b/driver/test/.gitignore new file mode 100644 index 0000000..ba2906d --- /dev/null +++ b/driver/test/.gitignore @@ -0,0 +1 @@ +main diff --git a/driver/test/main.c b/driver/test/main.c new file mode 100644 index 0000000..b87e56c --- /dev/null +++ b/driver/test/main.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include + +int main(int argc, char** argv) { + if (geteuid() != 0) { + fprintf(stderr, "run me as root!\n"); + return 1; + } + + argc--; // argv[0] is the program name + if (argc == 0) { + fprintf(stderr, "usage: %s /dev/lork\n", argv[0]); + return 1; + } + + char* dev = argv[1]; + const char* input = "test"; + int fd; + ssize_t bytes; + + fd = open(dev, O_RDWR); + if (-1 == fd) { + fprintf(stderr, "open() failed with code %d\n", errno); + return 1; + } + + bytes = write(fd, input, strlen(input)); + if (-1 == bytes) { + fprintf(stderr, "write() failed with code %d\n", errno); + return 1; + } + fprintf(stderr, "input \"%s\" to %s (%ld bytes succesful)\n", input, dev, bytes); + + char buf[80]; + bytes = read(fd, buf, 80); + if (-1 == bytes) { + fprintf(stderr, "read() failed with code %d\n", errno); + return 1; + } + fprintf(stderr, "output from %s: (%ld bytes)\n", dev, bytes); + if (bytes > 0) { + if (bytes < 80) buf[bytes] = '\0'; // null terminate string for printf + printf("%s\n", buf); + } + + if (-1 == close(fd)) { + fprintf(stderr, "close() failed with code %d\n", errno); + return 1; + } + + return 0; +} + diff --git a/driver/test/makefile b/driver/test/makefile new file mode 100644 index 0000000..3491973 --- /dev/null +++ b/driver/test/makefile @@ -0,0 +1,2 @@ +main: main.o + diff --git a/driver/test/test.sh b/driver/test/test.sh new file mode 100755 index 0000000..7dba005 --- /dev/null +++ b/driver/test/test.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +if [ $(id -u) -ne 0 ] ; then + echo "run me as root!" >&2 + exit 1 +fi + +if [ $# -eq 0 ] ; then + echo "usage: $0 /dev/lork" >&2 + exit 1 +fi + +DEV="$1" +INPUT="test" + +echo "input \"$INPUT\" to $DEV" >&2 +echo "$INPUT" > "$DEV" +echo "(EC $?)" >&2 + +echo "output from $DEV:" >&2 +cat "$DEV" +echo "(EC $?)" >&2 + +exit 0 -- cgit v1.2.3