Skip to content

Commit

Permalink
o Add working emulated RTC (Real Time Clock), some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Miller committed Nov 25, 2023
1 parent 2f276c8 commit 72accec
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 20 deletions.
7 changes: 6 additions & 1 deletion app/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "fs/dyndev.h"
#include "fs/devices.h"
#include "fs/path.h"
#include "app/RTCDevice.h"

#if ISH_LINUX
#import "LinuxInterop.h"
Expand Down Expand Up @@ -173,7 +174,11 @@ - (intptr_t)boot {
if (err != 0)
return err;
generic_mknodat(AT_PWD, "/dev/location", S_IFCHR|0666, dev_make(DYN_DEV_MAJOR, DEV_LOCATION_MINOR));
// The following does nothing for now. Placeholder

// Emulate a RTC, read time only
err = dyn_dev_register(&rtc_dev, DEV_CHAR, DEV_RTC_MAJOR, DEV_RTC_MINOR);
if (err != 0)
return err;
generic_mknodat(AT_PWD, "/dev/rtc0", S_IFCHR|0666, dev_make(DEV_RTC_MAJOR, DEV_RTC_MINOR));
generic_symlinkat("/dev/rtc0", AT_PWD, "/dev/rtc");

Expand Down
8 changes: 8 additions & 0 deletions app/RTCDevice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// RTCDevice.h
// iSH-AOK
//
// Created by Michael Miller on 11/24/23.
//

extern struct dev_ops rtc_dev;
116 changes: 116 additions & 0 deletions app/RTCDevice.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//
// RTCDevice.m
// iSH-AOK
//
// Created by Michael Miller on 11/24/23.
//

#import <Foundation/Foundation.h>
#include "fs/poll.h"
#include "fs/dyndev.h"
#include "kernel/errno.h"
#include "debug.h"
#include "fs/devices.h"

// Real Time Clock file descriptor structure
typedef struct fd rtc_fd;

// Need to define this, as we are running on iOS, which doesn't have/give access to the RTC
typedef struct rtc_time {
int tm_sec; // seconds
int tm_min; // minutes
int tm_hour; // hours
int tm_mday; // day of the month
int tm_mon; // month
int tm_year; // year
} rtc_time;

// Get the time, put it in the appropriate structure
static rtc_time *get_current_time(rtc_fd *fd, size_t *len) {
// Obtain the current date
NSDate *currentDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];

// Define the desired date components
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond)
fromDate:currentDate];

// Allocate and populate the rtc_time structure
rtc_time *timeStruct = malloc(sizeof(rtc_time));
if (!timeStruct) {
// Handle memory allocation failure
*len = 0;
return NULL;
}

// Populate the structure
// Note: tm_mon is 0-based (0 for January) and tm_year is years since 1900
timeStruct->tm_sec = (int)[components second];
timeStruct->tm_min = (int)[components minute];
timeStruct->tm_hour = (int)[components hour];
timeStruct->tm_mday = (int)[components day];
timeStruct->tm_mon = (int)[components month] - 1; // Adjust for tm_mon
timeStruct->tm_year = (int)[components year] - 1900; // Adjust for tm_year

// Update the size
*len = sizeof(rtc_time);

return timeStruct;
}

static ssize_t rtc_read(rtc_fd *fd, void *buf, size_t bufsize) {
return 0;
}

static int rtc_poll(rtc_fd *fd) {
return 0;
}

static int rtc_open(int major, int minor, rtc_fd *fd) {
return 0;
}

static int rtc_close(rtc_fd *fd) {
return 0;
}

#define RTC_RD_TIME 0x80247009 // Example definition, adjust as necessary

static ssize_t rtc_ioctl_size(int cmd) {
switch (cmd) {
case RTC_RD_TIME:
return sizeof(rtc_time);
}
return -1;
}

// Function to handle ioctl operations
static int rtc_ioctl(struct fd *fd, int cmd, void *arg) {
@autoreleasepool {
switch (cmd) {
case RTC_RD_TIME: { // On a real Linux, there are a number of other possible ioctl()'s. We don't really need them
size_t length = 0;
rtc_time *data = get_current_time(fd, &length);

if (arg == NULL) {
return _EFAULT; // Null pointer argument
}

*(rtc_time *) arg = *data; // This is the magic that gets the value back to the "kernel"

return 0; // Success
}
default:
return _EFAULT; // Oops
}
}
}

struct dev_ops rtc_dev = {
.open = rtc_open,
.fd.read = rtc_read,
.fd.poll = rtc_poll,
.fd.close = rtc_close,
.fd.ioctl = rtc_ioctl,
.fd.ioctl_size = rtc_ioctl_size, // Do NOT FORGET THIS if you want to implement ioctl(s) for your device. They will not work without it.
};
3 changes: 2 additions & 1 deletion fs/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "fs/tty.h"
#include "fs/dyndev.h"
#include "fs/devices.h"
#include "app/RTCDevice.h"

struct dev_ops *block_devs[256] = {
// no block devices yet
Expand All @@ -15,7 +16,7 @@ struct dev_ops *char_devs[256] = {
[TTY_ALTERNATE_MAJOR] = &tty_dev,
[TTY_PSEUDO_MASTER_MAJOR] = &tty_dev,
[TTY_PSEUDO_SLAVE_MAJOR] = &tty_dev,
// [DEV_RTC_MAJOR] = &rtc_dev_char,
[DEV_RTC_MAJOR] = &rtc_dev,
[DYN_DEV_MAJOR] = &dyn_dev_char,
};

Expand Down
6 changes: 0 additions & 6 deletions fs/dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ static inline dev_t_ dev_fake_from_real(dev_t dev) {
#define DEV_BLOCK 0
#define DEV_CHAR 1

struct dev_rtc {
intptr_t (*open)(int major, int minor, struct fd *fd);
int (*close)(int major, int minor, struct fd *fd);
ssize_t (*read)(void *buf, size_t count);
};

struct dev_ops {
intptr_t (*open)(int major, int minor, struct fd *fd);
struct fd_ops fd;
Expand Down
2 changes: 1 addition & 1 deletion fs/devices.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#define DYN_DEV_MAJOR 240
// /dev/rtc
#define DEV_RTC_MAJOR 252
#define DEV_RTC_MINOR 0
#define DEV_RTC_MINOR 2


// /dev/clipboard
Expand Down
2 changes: 1 addition & 1 deletion fs/dyndev.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int dyn_dev_register(struct dev_ops *ops, int type, int major, int minor) {
if (minor < 0 || minor > MAX_MINOR) {
return _EINVAL;
}
if (major != DYN_DEV_MAJOR) {
if ((major != DYN_DEV_MAJOR) && (major != DEV_RTC_MAJOR)) {
return _EINVAL;
}
if (ops == NULL) {
Expand Down
10 changes: 10 additions & 0 deletions iSH-AOK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@
5D8ACEFA284BF122003C50D3 /* net.c in Sources */ = {isa = PBXBuildFile; fileRef = 5D8ACEF9284BF122003C50D3 /* net.c */; };
5D8ACEFD284CE096003C50D3 /* sys.c in Sources */ = {isa = PBXBuildFile; fileRef = 5D8ACEFC284CE096003C50D3 /* sys.c */; };
5D8ACEFE284CE096003C50D3 /* sys.c in Sources */ = {isa = PBXBuildFile; fileRef = 5D8ACEFC284CE096003C50D3 /* sys.c */; };
5D8CFA852B1198B300D50E57 /* RTCDevice.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D8CFA842B1198B300D50E57 /* RTCDevice.m */; };
5D8CFA862B1198B300D50E57 /* RTCDevice.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D8CFA842B1198B300D50E57 /* RTCDevice.m */; };
5D8CFA872B1198B300D50E57 /* RTCDevice.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D8CFA842B1198B300D50E57 /* RTCDevice.m */; };
5D9897D028B6B953003D3670 /* AppStore.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 5D9897CE28B6B953003D3670 /* AppStore.xcconfig */; };
5DA0A8342AAE21D000397280 /* BatteryStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DA0A8332AAE21D000397280 /* BatteryStatus.m */; };
5DD383EB2AAE33330013A847 /* UIDevice.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DD383EA2AAE33330013A847 /* UIDevice.m */; };
Expand Down Expand Up @@ -589,6 +592,8 @@
5D8ACEFC284CE096003C50D3 /* sys.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sys.c; sourceTree = "<group>"; };
5D8C94D3287E53AD0095EDEE /* fp_sync.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = fp_sync.c; sourceTree = "<group>"; };
5D8CEFF92894C81400D24ED8 /* resource_locking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = resource_locking.h; sourceTree = "<group>"; };
5D8CFA842B1198B300D50E57 /* RTCDevice.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RTCDevice.m; sourceTree = "<group>"; };
5D8CFA882B1198DB00D50E57 /* RTCDevice.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RTCDevice.h; sourceTree = "<group>"; };
5D9897CE28B6B953003D3670 /* AppStore.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = AppStore.xcconfig; sourceTree = "<group>"; };
5D9897CF28B6B953003D3670 /* ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
5DA0A8332AAE21D000397280 /* BatteryStatus.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BatteryStatus.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1090,6 +1095,8 @@
BB88F4A4215476BA00A341FD /* iSH.entitlements */,
BB88F4912154760800A341FD /* FileProvider */,
BB41591D255EF9E300E0950C /* UITests */,
5D8CFA842B1198B300D50E57 /* RTCDevice.m */,
5D8CFA882B1198DB00D50E57 /* RTCDevice.h */,
);
path = app;
sourceTree = "<group>";
Expand Down Expand Up @@ -2122,6 +2129,7 @@
buildActionMask = 2147483647;
files = (
5DA0A8342AAE21D000397280 /* BatteryStatus.m in Sources */,
5D8CFA852B1198B300D50E57 /* RTCDevice.m in Sources */,
5DD383EB2AAE33330013A847 /* UIDevice.m in Sources */,
BB28C7BB268975AE00BDC834 /* LocationDevice.m in Sources */,
5D8ACEFD284CE096003C50D3 /* sys.c in Sources */,
Expand All @@ -2147,6 +2155,7 @@
BB88F49A2154760800A341FD /* FileProviderEnumerator.m in Sources */,
BBBFE94921C5CFF100509DD5 /* NSError+ISHErrno.m in Sources */,
BB9C7B87240A2B1E00F5D4F0 /* AppGroup.m in Sources */,
5D8CFA872B1198B300D50E57 /* RTCDevice.m in Sources */,
BB88F4942154760800A341FD /* FileProviderExtension.m in Sources */,
BB88F4972154760800A341FD /* FileProviderItem.m in Sources */,
);
Expand All @@ -2172,6 +2181,7 @@
BB28C79226896B1F00BDC834 /* AboutAppearanceViewController.m in Sources */,
BB28C79326896B1F00BDC834 /* AltIconViewController.m in Sources */,
491B31E62883BF22008EEFB0 /* ThemeViewController.m in Sources */,
5D8CFA862B1198B300D50E57 /* RTCDevice.m in Sources */,
BB28C7B226896C4600BDC834 /* main.m in Sources */,
496C7AB227CC697E005D7613 /* Theme.m in Sources */,
BB28C79426896B1F00BDC834 /* AboutNavigationController.m in Sources */,
Expand Down
20 changes: 10 additions & 10 deletions util/sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ static inline void complex_lockt(lock_t *lock, int log_lock, __attribute__((unus
}

lock->owner = pthread_self();
lock->pid = current_pid();
lock->uid = current_uid();
strncpy(lock->comm, current_comm(), sizeof(lock->comm) - 1);
lock->comm[sizeof(lock->comm) - 1] = '\0'; // Null-terminate just in case
//lock->pid = current_pid();
//lock->uid = current_uid();
//strncpy(lock->comm, current_comm(), sizeof(lock->comm) - 1);
//lock->comm[sizeof(lock->comm) - 1] = '\0'; // Null-terminate just in case
}

static inline void __lock(lock_t *lock, int log_lock, __attribute__((unused)) const char *file, __attribute__((unused)) int line) {
Expand All @@ -174,16 +174,16 @@ static inline void __lock(lock_t *lock, int log_lock, __attribute__((unused)) co
pthread_mutex_lock(&lock->m);
modify_locks_held_count_wrapper(1);
lock->owner = pthread_self();
lock->pid = current_pid();
lock->uid = current_uid();
strncpy(lock->comm, current_comm(), 16);
//lock->pid = current_pid();
//lock->uid = current_uid();
//strncpy(lock->comm, current_comm(), 16);
modify_critical_region_counter_wrapper(-1, __FILE__, __LINE__);
} else {
pthread_mutex_lock(&lock->m);
lock->owner = pthread_self();
lock->pid = current_pid();
lock->uid = current_uid();
strncpy(lock->comm, current_comm(), 16);
//lock->pid = current_pid();
//lock->uid = current_uid();
//strncpy(lock->comm, current_comm(), 16);
}
return;
}
Expand Down

0 comments on commit 72accec

Please sign in to comment.