From 62b6f1d20654b74d01056f31d6b45306c19fc29b Mon Sep 17 00:00:00 2001 From: caped-crusader16 Date: Thu, 3 Dec 2020 11:24:29 +0530 Subject: [PATCH 1/3] Fixing ATOI function --- apps/echo/stdlib.c | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/apps/echo/stdlib.c b/apps/echo/stdlib.c index f4013b2..5eda781 100644 --- a/apps/echo/stdlib.c +++ b/apps/echo/stdlib.c @@ -27,30 +27,26 @@ int atoi(const char *nptr) { - char *nstr = (char *)nptr; - char isneg = 0; - int sum = 0; - int mult = 1; + int sum = 0; - if(*nstr == '-') { - isneg = 1; - nstr++; - } + int sign = 0; - while(*nstr++) { - if(*nstr && !isdigit(*nstr)) - return(0); - } + int i = 0; - nstr--; - while(nstr-- != (nptr + isneg)) { - sum += (*nstr - '0') * mult; - mult *= 10; + if (nptr[0] == '-') { + sign = 1; + i++; + } + + while(nptr[i] != '\0'){ + if(!(nptr[i]>='0' && nptr[i]<='9')) + return __INT32_MAX__; + sum = (sum * 10) + (nptr[i] - '0'); + ++i; } - if(isneg) - return(sum * -1); - else - return(sum); + if(sign==1) + return (-1) * sum; + return sum; } From 3695cdaff71762d498867ca154da5417074c2c64 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 4 Dec 2020 01:21:39 +0530 Subject: [PATCH 2/3] 'Time' Command --- apps/time/Makefile | 25 ++++ apps/time/crt0.s | 9 ++ apps/time/minilibc.h | 18 +++ apps/time/printf.c | 326 +++++++++++++++++++++++++++++++++++++++++++ apps/time/stdarg.h | 12 ++ apps/time/stdlib.c | 59 ++++++++ apps/time/syscalls.c | 28 ++++ apps/time/time.c | 75 ++++++++++ 8 files changed, 552 insertions(+) create mode 100644 apps/time/Makefile create mode 100644 apps/time/crt0.s create mode 100644 apps/time/minilibc.h create mode 100644 apps/time/printf.c create mode 100644 apps/time/stdarg.h create mode 100644 apps/time/stdlib.c create mode 100644 apps/time/syscalls.c create mode 100644 apps/time/time.c diff --git a/apps/time/Makefile b/apps/time/Makefile new file mode 100644 index 0000000..dd385f1 --- /dev/null +++ b/apps/time/Makefile @@ -0,0 +1,25 @@ +## +# Makefile for calc. +# +CC=gcc +CFLAGS=-m32 -Os -mpush-args -fno-pie -fomit-frame-pointer -fno-builtin -nostartfiles -nostdlib -nodefaultlibs +AS=as +LD=ld +OUTPUT=time + +OBJECTS=crt0.o syscalls.o printf.o stdlib.o time.o + +all: $(OBJECTS) + $(LD) $(OBJECTS) -melf_i386 -Ttext=C0000C --oformat=binary -o $(OUTPUT) + #$(LD) $(OBJECTS) -melf_i386 -o $(OUTPUT) + +%.o: %.c + $(CC) $(CFLAGS) -c $< + +%.o: %.s + $(AS) --32 $< -o $@ + +clean: + @rm $(OBJECTS) || true + @[ -f $(OUTPUT) ] && rm $(OUTPUT) || true + diff --git a/apps/time/crt0.s b/apps/time/crt0.s new file mode 100644 index 0000000..9ff1007 --- /dev/null +++ b/apps/time/crt0.s @@ -0,0 +1,9 @@ +.global _start,main + +.text +_start: + call main + +loop: + jmp loop + diff --git a/apps/time/minilibc.h b/apps/time/minilibc.h new file mode 100644 index 0000000..34cf1df --- /dev/null +++ b/apps/time/minilibc.h @@ -0,0 +1,18 @@ + +#ifndef _MINILIBC +#define _MINILIBC + +#include "stdarg.h" + +#define CHECK_BIT(a, b) ((a >> b) & 0x01) +#define SET_BIT(a, b) a |= (0x01 << b) + +long write(int fd, void *buf, unsigned long count); +long read(int fd, void *buf, unsigned long count); +int vsprintf(char *str, const char *format, va_list ap); +int sprintf(char *str, const char *format, ...); +int _printf(const char *format, ...); +int atoi(const char *nptr); + +#endif + diff --git a/apps/time/printf.c b/apps/time/printf.c new file mode 100644 index 0000000..4e61f48 --- /dev/null +++ b/apps/time/printf.c @@ -0,0 +1,326 @@ +/* + * Copyright (C) 2009 RenĂª de Souza Pinto + * Tempos - Tempos is an Educational and multi purpose Operating System + * + * File: printf.c + * Desc: Implements the printf family C functions + * + * This file is part of TempOS. + * + * TempOS is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * TempOS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "minilibc.h" + +#define MAXDIG 20 + +#define FLAG_ALT 0x00 +#define FLAG_ZPAD 0x01 +#define FLAG_FB 0x02 +#define FLAG_SPACE 0x03 +#define FLAG_PLUS 0x04 + +/** Signed conversion */ +#define SIGNED 1 +/** Unsigned conversion */ +#define UNSIGNED 0 + +#define MAX_PRINTF_SIZE 1024 + +static void numtostr(char **dest, char flags, long int value, int base, int prec, char sig); + + +/** + * Convert number to string (in specific base) + * \param dest Destination of converted string + * \param flags Flags + * \param value Numeric value + * \param base The base (2 = binary / 10 = decimal / 16 = hexa) + * \param prec Precision + * \param sig Signed conversion (0 = unsigned) + */ +static void numtostr(char **dest, char flags, long int value, int base, int prec, char sig) +{ + char *ndest = *dest; + long int pos, div, quo; + unsigned long int udiv, uquo, rem; + char temp, sch, fch; + int i, sh, len; + + /* This is painful, there is a better way? */ + pos = 0; + if(sig) { + /* Signed conversion */ + div = value < 0 ? (value * -1) : value; /* Dividend (absolute) */ + quo = 1; /* Quotient */ + rem = 0; /* Remainder */ + if(base != 16) { + while(quo != 0) { + quo = (div / base); + rem = (div % base); + div = quo; + ndest[pos] = (rem + '0'); + pos++; + } + } else { + while(quo != 0) { + quo = (div / base); + rem = (div % base); + div = quo; + if(rem >= 10) { + ndest[pos] = (rem - 10 + 'A'); + } else { + ndest[pos] = (rem + '0'); + } + pos++; + } + } + } else { + /* Unsigned conversion */ + udiv = (unsigned)value; /* Dividend (absolute) */ + uquo = 1; /* Quotient */ + rem = 0; /* Remainder */ + if(base != 16) { + while(uquo != 0) { + uquo = (udiv / base); + rem = (udiv % base); + udiv = uquo; + ndest[pos] = (rem + '0'); + pos++; + } + } else { + while(uquo != 0) { + uquo = (udiv / base); + rem = (udiv % base); + udiv = uquo; + if(rem >= 10) { + ndest[pos] = (rem - 10 + 'A'); + } else { + ndest[pos] = (rem + '0'); + } + pos++; + } + } + } + pos--; + for(i=0; i<=(pos / 2); i++) { + temp = ndest[i]; + ndest[i] = ndest[pos-i]; + ndest[pos-i] = temp; + } + + /* Signal */ + sh = 0; + sch = 0; + if( CHECK_BIT(flags, FLAG_SPACE) || CHECK_BIT(flags, FLAG_PLUS) ) { + sh = 1; + if(value >= 0) { + if( CHECK_BIT(flags, FLAG_PLUS) ) { + sch = '+'; + } else { + sch = ' '; + } + } else { + if(sig) + sch = '-'; + } + } + + /* Shift */ + fch = ' '; + pos++; + if(prec > 0) { + len = (prec - pos); + if(len > 0) + sh += len; + + if( CHECK_BIT(flags, FLAG_ZPAD) ) + fch = '0'; + } + if(sh > 0) { + for(i=pos; i>=0; i--) + ndest[i + sh] = ndest[i]; + + for(i=0; i= '0' && *fmt <= '9')) + num[p++] = *fmt++; + num[p] = '\0'; + + /* Precision */ + if(*fmt == '.') { + fmt++; + while(*fmt && (*fmt >= '0' && *fmt <= '9')) + num[p++] = *fmt++; + num[p] = '\0'; + } + + /* Length modifier */ + switch(*fmt) { + case 'l': + fmt++; + break; + } + + /* Modifier */ + switch(*fmt) { + case 'i': + case 'd': + numtostr(&nstr, flags, (int)(va_arg(ap, int)), 10, atoi(num), SIGNED); + fmt++; + break; + + case 'u': + numtostr(&nstr, flags, (unsigned int)(va_arg(ap, unsigned int)), 10, atoi(num), UNSIGNED); + fmt++; + break; + + case 'o': + numtostr(&nstr, flags, (int)(va_arg(ap, int)), 8, atoi(num), SIGNED); + fmt++; + break; + + case 'x': + numtostr(&nstr, flags, (int)(va_arg(ap, int)), 16, atoi(num), SIGNED); + fmt++; + break; + + case 'c': + *nstr++ = (int)(va_arg(ap, int)); + fmt++; + break; + + case 's': + src = (char*)(va_arg(ap, char*)); + while(*src) + *nstr++ = *src++; + fmt++; + break; + + } + break; + } + } + } + *nstr = '\0'; + + return(nstr - str); +} + + +/** + * Formatted output conversion + * \param str Destination string + * \param format Format + * \param ... + */ +int sprintf(char *str, const char *format, ...) +{ + va_list args; + int res; + + va_start(args, format); + res = vsprintf(str, format, args); + va_end(args); + return(res); +} + + +/** + * Write formated messages. + */ +int _printf(const char *format, ...) +{ + va_list args; + int i, res; + char str[MAX_PRINTF_SIZE]; + + va_start(args, format); + res = vsprintf(str, format, args); + va_end(args); + + i = 0; + while (str[i] != '\0' && i < res) { + write(1, &str[i], 1); + i++; + } + + return(i); +} + diff --git a/apps/time/stdarg.h b/apps/time/stdarg.h new file mode 100644 index 0000000..a92367b --- /dev/null +++ b/apps/time/stdarg.h @@ -0,0 +1,12 @@ +#ifndef _STDARG_H +#define _STDARG_H + +typedef __builtin_va_list va_list; + +#define va_start(v,l) __builtin_va_start(v,l) +#define va_end(v) __builtin_va_end(v) +#define va_arg(v,l) __builtin_va_arg(v,l) +#define __va_copy(d,s) __builtin_va_copy(d,s) + +#endif + diff --git a/apps/time/stdlib.c b/apps/time/stdlib.c new file mode 100644 index 0000000..fcd48bc --- /dev/null +++ b/apps/time/stdlib.c @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2009 RenĂª de Souza Pinto + * Tempos - Tempos is an Educational and multi purpose Operating System + * + * File: stdlib.c + * Desc: Implements C functions from stdlib.h + * + * This file is part of TempOS. + * + * TempOS is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * TempOS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "stdlib.h" +#include "ctype.h" + +int atoi(const char *nptr) +{ + char *nstr = (char *)nptr; + char isneg = 0; + int sum = 0; + + if(*nstr == '-') { + isneg = 1; + nstr++; + } + + while(nstr++) { + if(*nstr && !(*nstr >= '0' && *nstr <= '9')) + return(0); + } + + *nstr = *nptr; + + if(isneg==1){ + nstr++; + } + + while(*nstr!='\0'){ + sum = sum * 10 + (*nstr - '0'); + nstr++; + } + + if(isneg) + return(sum * -1); + else + return(sum); +} diff --git a/apps/time/syscalls.c b/apps/time/syscalls.c new file mode 100644 index 0000000..e1ab9fd --- /dev/null +++ b/apps/time/syscalls.c @@ -0,0 +1,28 @@ +#include "minilibc.h" + +long write(int fd, void *buf, unsigned long count) +{ + long res; + + asm volatile("int $0x85" + : "=a" (res) + : "a" (4), "b"(fd), "c"(buf), "d"(count) + : "memory" + ); + + return res; +} + +long read(int fd, void *buf, unsigned long count) +{ + long res; + + asm volatile("int $0x85" + : "=a" (res) + : "a" (3), "b"(fd), "c"(buf), "d"(count) + : "memory" + ); + + return res; +} + diff --git a/apps/time/time.c b/apps/time/time.c new file mode 100644 index 0000000..1c39e66 --- /dev/null +++ b/apps/time/time.c @@ -0,0 +1,75 @@ +#include +#include "minilibc.h" + +#define outByte(value,port) \ +__asm__ ("outb %%al,%%dx\n" \ + "\tjmp 1f\n" \ + "1:\tjmp 1f\n" \ + "1:"::"a" (value),"d" (port)) + +#define inByte(port) ({ \ +unsigned char _v; \ +__asm__ volatile ("inb %%dx,%%al\n" \ + "\tjmp 1f\n" \ + "1:\tjmp 1f\n" \ + "1:":"=a" (_v):"d" (port)); \ +_v; \ +}) + +#define CMOS_READ(addr) ({ \ + outByte(addr,0x70); \ + inByte(0x71); \ +}) + +int bcd_to_bin(int unit, int key); +int convert_24h(int hour); + +void main() { + // reading values from associated CMOS register number containing respective data + + int hour; + int minute; + int second; + int day; + int month; + int year; + + second = CMOS_READ(0x00); + second=bcd_to_bin(second,0); + + minute = CMOS_READ(0x02); + minute=bcd_to_bin(minute,0); + + hour = CMOS_READ(0x04); + hour=bcd_to_bin(hour, 1); + + day = CMOS_READ(0x07); + day=bcd_to_bin(day, 0); + + month = CMOS_READ(0x08); + month=bcd_to_bin(month, 0); + + year = CMOS_READ(0x09); + year=bcd_to_bin(year, 0); + /* + if(val==1){ + hour= convert_24h(hour); + }*/ + + printf("%03i-%03i-'%03i %03i:%03i:%03i UTC\n", day, month, year, hour, minute, second); +} + +int bcd_to_bin(int unit, int key){ + // Converting BCD to binary values + unit = ((unit & 0x0F) + ((unit / 16) * 10)); + if(key==0) + return unit; + return ((unit) | (unit & 0x80)); +} + +int convert_24h(int hour){ + // Converting Hours into 24 hour format + if (!(CMOS_READ(0x0B) & 0x02) && (hour & 0x80)) { + return ((hour & 0x7F) + 12) % 24; + } +} From aa040d329e911895b74bb7241fc660cee43f7c0f Mon Sep 17 00:00:00 2001 From: Manav Vagrecha Date: Fri, 4 Dec 2020 10:42:37 +0530 Subject: [PATCH 3/3] Fixed Crashing of GRUB2 --- kernel/scripts/mkdisk_img.sh | 62 +++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/kernel/scripts/mkdisk_img.sh b/kernel/scripts/mkdisk_img.sh index 013e692..60e0ed3 100755 --- a/kernel/scripts/mkdisk_img.sh +++ b/kernel/scripts/mkdisk_img.sh @@ -83,28 +83,28 @@ EOF # Make image using grub 2 use_grub2() { - mktemp=$(which mktemp) - if [ -n $mktemp ]; then - errorlog=$($mktemp) - else - errorlog="/tmp/$$" - fi - - echo -n " + Creating CD-ROM root directory..." - TMPDIR=$($mktemp -d) - if [ $? != 0 ]; then - TMPDIR=/tmp/$$-dir - mkdir $TMPDIR 2>>$errorlog - fi - check_result - - echo -n " + Copying TempOS kernel image..." - mkdir -p $TMPDIR/boot/grub - cp $TEMPOSFILE $TMPDIR/boot 2>>$errorlog - check_result - - echo -n " + Creating GRUB configuration file..." - cat > $TMPDIR/boot/grub/grub.cfg << EOF + mktemp=$(which mktemp) + if [ -n $mktemp ]; then + errorlog=$($mktemp) + else + errorlog="/tmp/$$" + fi + + echo -n " + Creating CD-ROM root directory..." + TMPDIR=$($mktemp -d) + if [ $? != 0 ]; then + TMPDIR=/tmp/$$-dir + mkdir $TMPDIR 2>>$errorlog + fi + check_result + + echo -n " + Copying TempOS kernel image..." + mkdir -p $TMPDIR/boot/grub + cp $TEMPOSFILE $TMPDIR/boot 2>>$errorlog + check_result + + echo -n " + Creating GRUB configuration file..." + cat > $TMPDIR/boot/grub/grub.cfg << EOF set menu_color_normal="white/blue" set menu_color_highlight="light-green/black" @@ -112,20 +112,22 @@ set menu_color_highlight="light-green/black" # TempOS # menuentry "TempOS" { - multiboot /boot/$(basename $TEMPOSFILE) root=3:1 init=/sbin/init + multiboot2 /boot/$(basename $TEMPOSFILE) root=3:1 init=/sbin/init + boot } menuentry "TempOS over serial" { - multiboot /boot/$(basename $TEMPOSFILE) root=3:1 init=/sbin/init kgdbwait=1 + multiboot2 /boot/$(basename $TEMPOSFILE) root=3:1 init=/sbin/init kgdbwait=1 + boot } EOF - check_result + check_result - echo -n " + Creating bootable GRUB ISO image..." - $GRUBMKR -o $IMGFILE $TMPDIR > $errorlog 2>&1 - check_result + echo -n " + Creating bootable GRUB ISO image..." + $GRUBMKR -o $IMGFILE $TMPDIR > $errorlog 2>&1 + check_result - rm -rf $TMPDIR 2> /dev/null - rm -f $errorlog 2> /dev/null + rm -rf $TMPDIR 2> /dev/null + rm -f $errorlog 2> /dev/null } # main