Skip to content

Commit 734c4ca

Browse files
committed
* Implemented some functions of string lib
* Moved reboot function to its own file * Updated Makefile * Removed system.c * Updated system.h
1 parent 0ebb087 commit 734c4ca

File tree

8 files changed

+213
-4
lines changed

8 files changed

+213
-4
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ ISOFILE=$(PROJECT).iso
3131

3232
OBJECTS= $(SRCDIR)/boot/boot.o \
3333
$(SRCDIR)/drivers/io/ports.o \
34-
$(SRCDIR)/common/system.o \
34+
$(SRCDIR)/drivers/power/reboot.o \
35+
$(SRCDIR)/common/libs/string.o \
3536
$(SRCDIR)/drivers/video/vga_text.o \
3637
$(SRCDIR)/drivers/io/serial.o \
3738
$(SRCDIR)/kernel/kernel.o

docs/changelog.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@
1919
* Implemented scrolling
2020
* Updated Makefile
2121
* Incremented kernel version
22-
* Added new welcome message
22+
* Added new welcome message
23+
24+
26-03-2023: 10:37 PM IST v0.23.03.2ALPR
25+
* Implemented some functions of string lib
26+
* Moved reboot function to its own file
27+
* Updated Makefile
28+
* Removed system.c
29+
* Updated system.h

src/common/libs/string.c

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#include "string.h"
2+
3+
int strlen(const char* s)
4+
{
5+
int ret = 0;
6+
for(ret = 0; s[ret] != '\0'; ret++);
7+
return ret;
8+
}
9+
10+
int memcmp(uint8_t* data1, uint8_t* data2, int n)
11+
{
12+
while(n--)
13+
{
14+
if(*data1 != *data2)
15+
return 0;
16+
17+
data1++;
18+
data2++;
19+
}
20+
return 1;
21+
}
22+
23+
void* memcpy(void* dst, const void* src, int n)
24+
{
25+
char* ret = (char*)dst;
26+
char* d = (char*)dst;
27+
char* s = (char*)src;
28+
29+
while(n--)
30+
{
31+
*d = *s;
32+
d++;
33+
s++;
34+
}
35+
36+
return ret;
37+
}
38+
39+
void* memset(void* dst, char val, int n)
40+
{
41+
char* ret = (char*)dst;
42+
char* d = (char*)dst;
43+
44+
while(n--)
45+
{
46+
*d++ = val;
47+
}
48+
49+
return (void*)ret;
50+
}
51+
52+
int strcpy(char* dst, const char* src)
53+
{
54+
int n = strlen(src);
55+
int ret = n;
56+
57+
while(n--)
58+
{
59+
*dst++ = *src++;
60+
}
61+
return ret;
62+
}
63+
64+
char* strncpy(char* dst, const char* src, int n)
65+
{
66+
unsigned count;
67+
if((dst == (char*)NULL) || (src == (char*)NULL))
68+
{
69+
return (dst == NULL);
70+
}
71+
72+
if(n > 255)
73+
{
74+
n = 255;
75+
}
76+
77+
for(count = 0; (int)count < n; count++)
78+
{
79+
dst[count] = src[count];
80+
81+
if(src[count] == '\0')
82+
break;
83+
}
84+
85+
if(count >= 255)
86+
{
87+
return (dst == NULL);
88+
}
89+
}
90+
91+
int strcmp(const char* dst, char* src)
92+
{
93+
int i = 0;
94+
while(dst[i] == src[i])
95+
{
96+
if(src[i++] == 0)
97+
return 0;
98+
}
99+
100+
return 1;
101+
}
102+
103+
int strncmp(const char *s1, const char *s2, int c)
104+
{
105+
int result = 0;
106+
107+
while (c)
108+
{
109+
result = *s1 - *s2++;
110+
111+
if ((result != 0) || (*s1++ == 0))
112+
{
113+
break;
114+
}
115+
116+
c--;
117+
}
118+
119+
return result;
120+
}
121+
122+
char* strstr(const char* in, const char* str)
123+
{
124+
char c;
125+
size_t len;
126+
127+
c = *str;
128+
if(!c)
129+
return (char*)in;
130+
131+
len = strlen(str);
132+
133+
do
134+
{
135+
char sc;
136+
do
137+
{
138+
sc = *in++;
139+
if (!sc)
140+
return (char *)0;
141+
} while (sc != c);
142+
} while (strncmp(in, str, len) != 0);
143+
144+
return (char *)(in - 1);
145+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#include "system.h"
2-
#include "ports.h"
1+
#include "power.h"
32

43
void reboot()
54
{
65
outb(0x64, 0xFE);
6+
khalt;
77
}

src/include/power.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef __POWER_H__
2+
#define __POWER_H__
3+
4+
#include "system.h"
5+
#include "ports.h"
6+
7+
void reboot();
8+
9+
#endif /*__POWER_H__*/

src/include/string.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef __STRING_H__
2+
#define __STRING_H__
3+
4+
#include "system.h"
5+
6+
int memcmp(uint8_t *data1, uint8_t *data2, int n); //* Implemented
7+
void *memcpy(void *dst, const void *src, int n); //* Implemented
8+
9+
uint16_t *memsetw(uint16_t *dest, uint16_t val, uint32_t count);
10+
uint16_t *memsetdw(uint32_t *dest, uint32_t val, uint32_t count);
11+
void *memset(void *dst, char val, int n); //* Implemented
12+
13+
int strlen(const char *s); //* Implemented
14+
15+
char *strncpy(char *destString, const char *sourceString, int maxLength); //* Implmented
16+
int strcpy(char *dst, const char *src); //* Implemented
17+
18+
int strcmp(const char *dst, char *src); //* Implemented
19+
int strncmp(const char *s1, const char *s2, int c); //* Implemented
20+
21+
char *strstr(const char *in, const char *str); //* Implemented
22+
void strcat(void *dest, const void *src);
23+
24+
char* itoa_r(unsigned long int n, int base);
25+
void itoa(char *buf, unsigned long int n, int base);
26+
int atoi(char *string);
27+
28+
int isspace(char c);
29+
int isprint(char c);
30+
31+
char *strdup(const char *src);
32+
char *strndup(const char *src, uint32_t len);
33+
char *strsep(char **stringp, const char *delim);
34+
35+
void stoc(size_t n, char* buffer);
36+
char* stoc_r(size_t n);
37+
uint32_t chbc(char* str, char c);
38+
39+
#endif /*__STRING_H__*/

src/include/system.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ void kernel_panic(char* message);
1515

1616
#define KERNEL_START 0x100000
1717
#define KERNEL_END k_end
18+
19+
#define NULL (void*)0
1820
/*
1921
* version system
2022
* REVN.YY.MM.RELN(STATUS)
@@ -28,4 +30,6 @@ void kernel_panic(char* message);
2830

2931
void reboot();
3032

33+
typedef uint32_t size_t;
34+
3135
#endif

src/kernel/kernel.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@
66
void kernelmain(const multiboot_info_t* info, uint32_t multiboot_magic)
77
{
88
init_serial(COM1, 1);
9+
910
init_text();
1011
text_chcolor(VGA_LIGHT_GREEN, VGA_BLACK);
1112
text_clear();
13+
1214
if(multiboot_magic != 0x2BADB002)
1315
{
1416
serial_puts("multiboot bootloader magic is invalid\n");
1517
return;
1618
}
19+
1720
text_puts("Hello World!\n");
1821
text_puts("multiboot bootloader magic is valid...\n");
1922
text_puts("Kernel version is: ");
2023
text_puts(KERNEL_VERSION);
2124
text_putc('\n');
25+
2226
khalt;
2327
}

0 commit comments

Comments
 (0)