Skip to content

Commit

Permalink
Add devices.h, replace magic dev numbers with defines
Browse files Browse the repository at this point in the history
  • Loading branch information
stek29 committed Aug 6, 2019
1 parent 26884b9 commit c11d375
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 57 deletions.
40 changes: 22 additions & 18 deletions app/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "kernel/calls.h"
#include "fs/path.h"
#include "fs/dyndev.h"
#include "fs/devices.h"

@interface AppDelegate ()

Expand Down Expand Up @@ -89,30 +90,33 @@ - (int)startThings {

// create some device nodes
// this will do nothing if they already exist
generic_mknod("/dev/console", S_IFCHR|0666, dev_make(5, 1));
generic_mknod("/dev/tty1", S_IFCHR|0666, dev_make(4, 1));
generic_mknod("/dev/tty2", S_IFCHR|0666, dev_make(4, 2));
generic_mknod("/dev/tty3", S_IFCHR|0666, dev_make(4, 3));
generic_mknod("/dev/tty4", S_IFCHR|0666, dev_make(4, 4));
generic_mknod("/dev/tty5", S_IFCHR|0666, dev_make(4, 5));
generic_mknod("/dev/tty6", S_IFCHR|0666, dev_make(4, 6));
generic_mknod("/dev/tty7", S_IFCHR|0666, dev_make(4, 7));
generic_mknod("/dev/tty", S_IFCHR|0666, dev_make(5, 0));
generic_mknod("/dev/ptmx", S_IFCHR|0666, dev_make(5, 2));
generic_mknod("/dev/null", S_IFCHR|0666, dev_make(1, 3));
generic_mknod("/dev/zero", S_IFCHR|0666, dev_make(1, 5));
generic_mknod("/dev/full", S_IFCHR|0666, dev_make(1, 7));
generic_mknod("/dev/random", S_IFCHR|0666, dev_make(1, 8));
generic_mknod("/dev/urandom", S_IFCHR|0666, dev_make(1, 9));
generic_mknod("/dev/tty1", S_IFCHR|0666, dev_make(TTY_CONSOLE_MAJOR, 1));
generic_mknod("/dev/tty2", S_IFCHR|0666, dev_make(TTY_CONSOLE_MAJOR, 2));
generic_mknod("/dev/tty3", S_IFCHR|0666, dev_make(TTY_CONSOLE_MAJOR, 3));
generic_mknod("/dev/tty4", S_IFCHR|0666, dev_make(TTY_CONSOLE_MAJOR, 4));
generic_mknod("/dev/tty5", S_IFCHR|0666, dev_make(TTY_CONSOLE_MAJOR, 5));
generic_mknod("/dev/tty6", S_IFCHR|0666, dev_make(TTY_CONSOLE_MAJOR, 6));
generic_mknod("/dev/tty7", S_IFCHR|0666, dev_make(TTY_CONSOLE_MAJOR, 7));

generic_mknod("/dev/tty", S_IFCHR|0666, dev_make(TTY_ALTERNATE_MAJOR, DEV_TTY_MINOR));
generic_mknod("/dev/console", S_IFCHR|0666, dev_make(TTY_ALTERNATE_MAJOR, DEV_CONSOLE_MINOR));
generic_mknod("/dev/ptmx", S_IFCHR|0666, dev_make(TTY_ALTERNATE_MAJOR, DEV_PTMX_MINOR));

generic_mknod("/dev/null", S_IFCHR|0666, dev_make(MEM_MAJOR, DEV_NULL_MINOR));
generic_mknod("/dev/zero", S_IFCHR|0666, dev_make(MEM_MAJOR, DEV_ZERO_MINOR));
generic_mknod("/dev/full", S_IFCHR|0666, dev_make(MEM_MAJOR, DEV_FULL_MINOR));
generic_mknod("/dev/random", S_IFCHR|0666, dev_make(MEM_MAJOR, DEV_RANDOM_MINOR));
generic_mknod("/dev/urandom", S_IFCHR|0666, dev_make(MEM_MAJOR, DEV_URANDOM_MINOR));

generic_mkdirat(AT_PWD, "/dev/pts", 0755);

// Register clipboard device driver and create device node for it
err = dyn_dev_register(&clipboard_dev, DYN_DEV_TYPE_CHAR, DYN_DEV_MAJOR, CLIPBOARD_DEV_MINOR);
err = dyn_dev_register(&clipboard_dev, DEV_CHAR, DYN_DEV_MAJOR, DEV_CLIPBOARD_MINOR);
if (err != 0) {
return err;
}
generic_mknod("/dev/clipboard", S_IFCHR|0666, dev_make(DYN_DEV_MAJOR, CLIPBOARD_DEV_MINOR));
generic_mknod("/dev/clipboard", S_IFCHR|0666, dev_make(DYN_DEV_MAJOR, DEV_CLIPBOARD_MINOR));

do_mount(&procfs, "proc", "/proc", 0);
do_mount(&devptsfs, "devpts", "/dev/pts", 0);

Expand Down
3 changes: 0 additions & 3 deletions app/Pasteboard.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
// Pasteboard is implementation of /dev/clipboard device

#define CLIPBOARD_DEV_MINOR 0

extern struct dev_ops clipboard_dev;
7 changes: 4 additions & 3 deletions app/TerminalViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "ArrowBarButton.h"
#import "UserPreferences.h"
#import "AboutViewController.h"
#include "fs/devices.h"

@interface TerminalViewController () <UIGestureRecognizerDelegate>

Expand Down Expand Up @@ -39,9 +40,9 @@ @implementation TerminalViewController
- (void)viewDidLoad {
[super viewDidLoad];
if (UserPreferences.shared.bootEnabled) {
self.terminal = [Terminal terminalWithType:4 number:7];
self.terminal = [Terminal terminalWithType:TTY_CONSOLE_MAJOR number:7];
} else {
self.terminal = [Terminal terminalWithType:4 number:1];
self.terminal = [Terminal terminalWithType:TTY_CONSOLE_MAJOR number:1];
}
[self.termView becomeFirstResponder];

Expand Down Expand Up @@ -222,7 +223,7 @@ - (IBAction)pressArrow:(ArrowBarButton *)sender {

- (void)switchTerminal:(UIKeyCommand *)sender {
unsigned i = (unsigned) sender.input.integerValue;
self.terminal = [Terminal terminalWithType:4 number:i];
self.terminal = [Terminal terminalWithType:TTY_CONSOLE_MAJOR number:i];
}

- (NSArray<UIKeyCommand *> *)keyCommands {
Expand Down
5 changes: 3 additions & 2 deletions fs/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
#include "fs/mem.h"
#include "fs/tty.h"
#include "fs/dyndev.h"
#include "fs/devices.h"

struct dev_ops *block_devs[256] = {
// no block devices yet
};
struct dev_ops *char_devs[256] = {
[1] = &mem_dev,
[MEM_MAJOR] = &mem_dev,
[TTY_CONSOLE_MAJOR] = &tty_dev,
[5] = &tty_dev,
[TTY_ALTERNATE_MAJOR] = &tty_dev,
[TTY_PSEUDO_MASTER_MAJOR] = &tty_dev,
[TTY_PSEUDO_SLAVE_MAJOR] = &tty_dev,
[DYN_DEV_MAJOR] = &dyn_dev_char,
Expand Down
42 changes: 42 additions & 0 deletions fs/devices.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef FS_DEVICES_H
#define FS_DEVICES_H

// losely based on devices.txt from linux

// --- memory devices ---
#define MEM_MAJOR 1
// /dev/null
#define DEV_NULL_MINOR 3
// /dev/zero
#define DEV_ZERO_MINOR 5
// /dev/full
#define DEV_FULL_MINOR 7
// /dev/random
#define DEV_RANDOM_MINOR 8
// /dev/urandom
#define DEV_URANDOM_MINOR 9

// --- tty devices ---
// /dev/ttyX where X is minor
#define TTY_CONSOLE_MAJOR 4

// --- alternate tty devices ---
#define TTY_ALTERNATE_MAJOR 5
// /dev/tty
#define DEV_TTY_MINOR 0
// /dev/console
#define DEV_CONSOLE_MINOR 1
// /dev/ptmx
#define DEV_PTMX_MINOR 2

// --- pseudo tty devices ---
#define TTY_PSEUDO_MASTER_MAJOR 128
#define TTY_PSEUDO_SLAVE_MAJOR 136

// --- dynamic devices ---
#define DYN_DEV_MAJOR 240

// /dev/clipboard
#define DEV_CLIPBOARD_MINOR 0

#endif
8 changes: 4 additions & 4 deletions fs/dyndev.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "kernel/errno.h"
#include "fs/dev.h"
#include "fs/dyndev.h"

#include "fs/devices.h"

#define MAX_MINOR 255

Expand Down Expand Up @@ -30,7 +30,7 @@ int dyn_dev_register(struct dev_ops *ops, int type, int major, int minor) {
if (ops == NULL) {
return _EINVAL;
}
if (type != DYN_DEV_TYPE_CHAR) {
if (type != DEV_CHAR) {
return _EINVAL;
}

Expand All @@ -49,7 +49,7 @@ int dyn_dev_register(struct dev_ops *ops, int type, int major, int minor) {
}

static int dyn_open(int type, int major, int minor, struct fd *fd) {
assert(type == DYN_DEV_TYPE_CHAR);
assert(type == DEV_CHAR);
assert(major == DYN_DEV_MAJOR);
// it's safe to access devs without locking (read-only)
struct dev_ops *ops = dyn_info_char.devs[minor];
Expand All @@ -65,7 +65,7 @@ static int dyn_open(int type, int major, int minor, struct fd *fd) {
}

static int dyn_open_char(int major, int minor, struct fd *fd) {
return dyn_open(DYN_DEV_TYPE_CHAR, major, minor, fd);
return dyn_open(DEV_CHAR, major, minor, fd);
}
struct dev_ops dyn_dev_char = {
.open = dyn_open_char,
Expand Down
6 changes: 1 addition & 5 deletions fs/dyndev.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@
// with custom dev_ops assigned
// It's useful to add new device "drivers" in runtime (for example,
// devices only present on some platforms)
#define DYN_DEV_MAJOR 240

// dev_ops handing char device with DYN_DEV_MAJOR major number
extern struct dev_ops dyn_dev_char;

#define DYN_DEV_TYPE_CHAR 0
#define DYN_DEV_TYPE_BLOCK 1

// Registeres new block/character device with provided major and
// minor numbers, handled by provided ops
//
// ops should be valid for "kernel" lifetime (should not be freed, but
// might be static), and should not be null
//
// type is DYN_DEV_TYPE_CHAR or DYN_DEV_TYPE_BLOCK
// type is DEV_BLOCK or DEV_CHAR
// (only char is supported for now)
//
// major should be DYN_DEV_MAJOR
Expand Down
19 changes: 13 additions & 6 deletions fs/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
#include "fs/poll.h"
#include "fs/mem.h"
#include "fs/dev.h"
#include "fs/devices.h"

// this file handles major device number 1, minor device numbers are mapped in table below
extern struct dev_ops
null_dev,
zero_dev,
full_dev,
random_dev;

// this file handles major device number MEM_MAJOR, minor device numbers are mapped in table below
struct dev_ops *mem_devs[256] = {
// [1] = &prog_mem_dev,
// [2] = &kmem_dev, // (not really applicable)
[3] = &null_dev,
[DEV_NULL_MINOR] = &null_dev,
// [4] = &port_dev,
[5] = &zero_dev,
[7] = &full_dev,
[8] = &random_dev,
[9] = &random_dev,
[DEV_ZERO_MINOR] = &zero_dev,
[DEV_FULL_MINOR] = &full_dev,
[DEV_RANDOM_MINOR] = &random_dev,
[DEV_URANDOM_MINOR] = &random_dev,
// [10] = &aio_dev,
// [11] = &kmsg_dev,
// [12] = &oldmem_dev, // replaced by /proc/vmcore
Expand Down
7 changes: 1 addition & 6 deletions fs/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
#include "kernel/fs.h"
#include "fs/dev.h"

extern struct dev_ops
mem_dev,
null_dev,
zero_dev,
full_dev,
random_dev;
extern struct dev_ops mem_dev;

#endif
1 change: 1 addition & 0 deletions fs/pty.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "kernel/task.h"
#include "kernel/errno.h"
#include "fs/tty.h"
#include "fs/devices.h"

extern struct tty_driver pty_slave;

Expand Down
11 changes: 6 additions & 5 deletions fs/tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "kernel/calls.h"
#include "fs/poll.h"
#include "fs/tty.h"
#include "fs/devices.h"

extern struct tty_driver pty_master;
extern struct tty_driver pty_slave;
Expand Down Expand Up @@ -126,13 +127,13 @@ static void tty_set_controlling(struct tgroup *group, struct tty *tty) {
}

// by default, /dev/console is /dev/tty1
int console_major = 4;
int console_major = TTY_CONSOLE_MAJOR;
int console_minor = 1;

static int tty_open(int major, int minor, struct fd *fd) {
struct tty *tty;
if (major == 5) {
if (minor == 0) {
if (major == TTY_ALTERNATE_MAJOR) {
if (minor == DEV_TTY_MINOR) {
lock(&ttys_lock);
lock(&current->group->lock);
tty = current->group->tty;
Expand All @@ -145,9 +146,9 @@ static int tty_open(int major, int minor, struct fd *fd) {
unlock(&ttys_lock);
if (tty == NULL)
return _ENXIO;
} else if (minor == 1) {
} else if (minor == DEV_CONSOLE_MINOR) {
return tty_open(console_major, console_minor, fd);
} else if (minor == 2) {
} else if (minor == DEV_PTMX_MINOR) {
return ptmx_open(fd);
} else {
return _ENXIO;
Expand Down
4 changes: 0 additions & 4 deletions fs/tty.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ struct termios_ {
#define ONOCR_ (1 << 4)
#define ONLRET_ (1 << 5)

#define TTY_CONSOLE_MAJOR 4
#define TTY_PSEUDO_MASTER_MAJOR 128
#define TTY_PSEUDO_SLAVE_MAJOR 136

#define TCGETS_ 0x5401
#define TCSETS_ 0x5402
#define TCSETSW_ 0x5403
Expand Down
2 changes: 2 additions & 0 deletions iSH.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
650B336D22EA052400B4C03E /* dyndev.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = dyndev.h; sourceTree = "<group>"; };
650B337222EA235C00B4C03E /* Pasteboard.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Pasteboard.h; sourceTree = "<group>"; };
650B337322EA235C00B4C03E /* Pasteboard.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Pasteboard.m; sourceTree = "<group>"; };
650B337522EA728B00B4C03E /* devices.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = devices.h; sourceTree = "<group>"; };
8632A7BD219A59FB00F02325 /* UserPreferences.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UserPreferences.h; sourceTree = "<group>"; };
8632A7BE219A59FB00F02325 /* UserPreferences.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = UserPreferences.m; sourceTree = "<group>"; };
9A28E4E8219A8B670073D200 /* AppearanceViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppearanceViewController.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -501,6 +502,7 @@
BB7D936B2087C2890008DA78 /* adhoc.c */,
BB7D936C2087C2890008DA78 /* dev.c */,
BB7D936D2087C2890008DA78 /* dev.h */,
650B337522EA728B00B4C03E /* devices.h */,
BB7D936E2087C2890008DA78 /* dir.c */,
BB7D936F2087C2890008DA78 /* fake-rebuild.c */,
BB7D93702087C2890008DA78 /* fake.c */,
Expand Down
4 changes: 3 additions & 1 deletion kernel/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "kernel/calls.h"
#include "fs/fd.h"
#include "fs/tty.h"
#include "fs/devices.h"

int mount_root(const struct fs_ops *fs, const char *source) {
char source_realpath[MAX_PATH + 1];
Expand Down Expand Up @@ -122,7 +123,8 @@ int create_stdio(const char *file) {
if (IS_ERR(fd)) {
// fallback to adhoc files for stdio
fd = adhoc_fd_create(NULL);
fd->stat.rdev = dev_make(4, 1);
// /dev/tty1
fd->stat.rdev = dev_make(TTY_CONSOLE_MAJOR, 1);
fd->stat.mode = S_IFCHR | S_IRUSR;
fd->flags = O_RDWR_;
int err = dev_open(4, 1, DEV_CHAR, fd);
Expand Down
1 change: 1 addition & 0 deletions xX_main_Xx.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <errno.h>
#include "kernel/init.h"
#include "kernel/fs.h"
#include "fs/devices.h"
#ifdef __APPLE__
#include <sys/resource.h>
#define IOPOL_TYPE_VFS_HFS_CASE_SENSITIVITY 1
Expand Down

0 comments on commit c11d375

Please sign in to comment.