Compare commits
6 Commits
1e68ac7312
...
7b88b9cea3
Author | SHA1 | Date | |
---|---|---|---|
7b88b9cea3 | |||
54cc80f649 | |||
b8f81502b8 | |||
e8e05159c1 | |||
49a6c39c38 | |||
706752d6b9 |
@ -6,6 +6,7 @@
|
|||||||
#include "fs/MBR.h"
|
#include "fs/MBR.h"
|
||||||
#include "memory/MemoryManager.h"
|
#include "memory/MemoryManager.h"
|
||||||
#include <luna/Alignment.h>
|
#include <luna/Alignment.h>
|
||||||
|
#include <luna/Buffer.h>
|
||||||
#include <luna/CType.h>
|
#include <luna/CType.h>
|
||||||
#include <luna/SafeArithmetic.h>
|
#include <luna/SafeArithmetic.h>
|
||||||
#include <luna/Vector.h>
|
#include <luna/Vector.h>
|
||||||
@ -758,9 +759,7 @@ Result<u64> ATADevice::read(u8* buf, usize offset, usize size) const
|
|||||||
|
|
||||||
ScopedKMutexLock<100> lock(m_drive->channel()->lock());
|
ScopedKMutexLock<100> lock(m_drive->channel()->lock());
|
||||||
|
|
||||||
// FIXME: Don't always allocate this if we don't need it.
|
Buffer temp;
|
||||||
auto* temp = (u8*)TRY(malloc_impl(block_size));
|
|
||||||
auto guard = make_scope_guard([temp] { free_impl(temp); });
|
|
||||||
|
|
||||||
if (offset % block_size)
|
if (offset % block_size)
|
||||||
{
|
{
|
||||||
@ -769,8 +768,10 @@ Result<u64> ATADevice::read(u8* buf, usize offset, usize size) const
|
|||||||
// Maybe we don't even want enough to get to the next block?
|
// Maybe we don't even want enough to get to the next block?
|
||||||
if (extra_size > size) extra_size = size;
|
if (extra_size > size) extra_size = size;
|
||||||
|
|
||||||
TRY(m_drive->read_lba(offset / block_size, temp, 1));
|
TRY(temp.try_resize(block_size));
|
||||||
memcpy(buf, temp + (offset % block_size), extra_size);
|
|
||||||
|
TRY(m_drive->read_lba(offset / block_size, temp.data(), 1));
|
||||||
|
memcpy(buf, temp.data() + (offset % block_size), extra_size);
|
||||||
offset += extra_size;
|
offset += extra_size;
|
||||||
size -= extra_size;
|
size -= extra_size;
|
||||||
buf += extra_size;
|
buf += extra_size;
|
||||||
@ -794,8 +795,9 @@ Result<u64> ATADevice::read(u8* buf, usize offset, usize size) const
|
|||||||
|
|
||||||
if (size)
|
if (size)
|
||||||
{
|
{
|
||||||
TRY(m_drive->read_lba(offset / block_size, temp, 1));
|
TRY(temp.try_resize(block_size));
|
||||||
memcpy(buf, temp, size);
|
TRY(m_drive->read_lba(offset / block_size, temp.data(), 1));
|
||||||
|
memcpy(buf, temp.data(), size);
|
||||||
}
|
}
|
||||||
|
|
||||||
return length;
|
return length;
|
||||||
|
@ -66,6 +66,9 @@ namespace GPT
|
|||||||
{
|
{
|
||||||
header.checksum = 0;
|
header.checksum = 0;
|
||||||
|
|
||||||
return CRC32::checksum((u8*)&header, 0x5c);
|
CRC32 crc;
|
||||||
|
crc.append((u8*)&header, 0x5c);
|
||||||
|
|
||||||
|
return crc.digest();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,11 @@ Result<usize> Pipe::read(u8* buf, usize, usize length)
|
|||||||
|
|
||||||
Result<usize> Pipe::write(const u8* buf, usize, usize length)
|
Result<usize> Pipe::write(const u8* buf, usize, usize length)
|
||||||
{
|
{
|
||||||
if (!m_reader) return length;
|
if (!m_reader)
|
||||||
|
{
|
||||||
|
Scheduler::current()->send_signal(SIGPIPE);
|
||||||
|
return err(EPIPE);
|
||||||
|
}
|
||||||
|
|
||||||
u8* slice = TRY(m_data_buffer.slice_at_end(length));
|
u8* slice = TRY(m_data_buffer.slice_at_end(length));
|
||||||
memcpy(slice, buf, length);
|
memcpy(slice, buf, length);
|
||||||
|
@ -63,7 +63,7 @@ namespace Ext2
|
|||||||
|
|
||||||
#ifdef EXT2_DEBUG
|
#ifdef EXT2_DEBUG
|
||||||
kdbgln("ext2: Read inode %lu with mode %#x (%#x + %#o), size %lu", inum, inode->m_raw_inode.mode,
|
kdbgln("ext2: Read inode %lu with mode %#x (%#x + %#o), size %lu", inum, inode->m_raw_inode.mode,
|
||||||
inode->m_raw_inode.mode & 0xf000, inode->mode(), inode->size());
|
inode->m_raw_inode.mode & 0xf000, inode->metadata().mode, inode->metadata().size);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
m_inode_cache.try_set(inum, inode);
|
m_inode_cache.try_set(inum, inode);
|
||||||
|
@ -91,19 +91,18 @@ namespace Ext2
|
|||||||
const usize inode_size = m_metadata.size;
|
const usize inode_size = m_metadata.size;
|
||||||
const usize block_size = m_fs->m_block_size;
|
const usize block_size = m_fs->m_block_size;
|
||||||
|
|
||||||
u8* const buf = (u8*)TRY(calloc_impl(block_size, 1));
|
auto buf = TRY(Buffer::create_sized(block_size));
|
||||||
auto guard = make_scope_guard([buf] { free_impl(buf); });
|
|
||||||
|
|
||||||
m_entries.clear();
|
m_entries.clear();
|
||||||
|
|
||||||
for (usize offset = 0; offset < inode_size; offset += block_size)
|
for (usize offset = 0; offset < inode_size; offset += block_size)
|
||||||
{
|
{
|
||||||
TRY(read(buf, offset, block_size));
|
TRY(read(buf.data(), offset, block_size));
|
||||||
|
|
||||||
usize dir_offset = 0;
|
usize dir_offset = 0;
|
||||||
while (dir_offset < block_size)
|
while (dir_offset < block_size)
|
||||||
{
|
{
|
||||||
auto& entry = *(Ext2::RawDirectoryEntry*)&buf[dir_offset];
|
auto& entry = *(Ext2::RawDirectoryEntry*)&buf.data()[dir_offset];
|
||||||
|
|
||||||
if (entry.inum != 0)
|
if (entry.inum != 0)
|
||||||
{
|
{
|
||||||
|
@ -182,7 +182,7 @@ Result<u64> sys_fork(Registers* regs, SyscallArgs)
|
|||||||
Scheduler::add_thread(thread);
|
Scheduler::add_thread(thread);
|
||||||
|
|
||||||
#ifdef FORK_DEBUG
|
#ifdef FORK_DEBUG
|
||||||
kdbgln("fork: thread %lu forked into child %lu", current->id, thread->id);
|
kdbgln("fork: thread %d forked into child %d", current->id, thread->id);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return thread->id;
|
return thread->id;
|
||||||
|
@ -183,7 +183,7 @@ namespace Scheduler
|
|||||||
CPU::disable_interrupts();
|
CPU::disable_interrupts();
|
||||||
|
|
||||||
#ifdef REAP_DEBUG
|
#ifdef REAP_DEBUG
|
||||||
kdbgln("reap: reaping thread with id %zu", thread->id);
|
kdbgln("reap: reaping thread with id %d", thread->id);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (thread->is_kernel)
|
if (thread->is_kernel)
|
||||||
|
@ -27,6 +27,7 @@ typedef struct
|
|||||||
int mode; // The buffering mode.
|
int mode; // The buffering mode.
|
||||||
} _buf;
|
} _buf;
|
||||||
int _flags; // The file access mode with which the file was opened.
|
int _flags; // The file access mode with which the file was opened.
|
||||||
|
pid_t _pid; // For popen(3) files, the pid of the child process.
|
||||||
} FILE;
|
} FILE;
|
||||||
|
|
||||||
#define EOF -1
|
#define EOF -1
|
||||||
@ -52,6 +53,7 @@ extern "C"
|
|||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Flush a stream's buffers. */
|
||||||
int fflush(FILE*);
|
int fflush(FILE*);
|
||||||
|
|
||||||
/* Open a file and bind a stream to it. */
|
/* Open a file and bind a stream to it. */
|
||||||
@ -201,6 +203,9 @@ extern "C"
|
|||||||
/* Move a file's location across a file system. */
|
/* Move a file's location across a file system. */
|
||||||
int rename(const char* oldpath, const char* newpath);
|
int rename(const char* oldpath, const char* newpath);
|
||||||
|
|
||||||
|
/* Pipe a stream to or from a process. */
|
||||||
|
FILE* popen(const char* command, const char* type);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/syscall.h>
|
#include <sys/syscall.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
FILE* stdin = nullptr;
|
FILE* stdin = nullptr;
|
||||||
@ -761,4 +762,87 @@ extern "C"
|
|||||||
unlink(oldpath);
|
unlink(oldpath);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FILE* popen(const char* command, const char* type)
|
||||||
|
{
|
||||||
|
int pfds[2];
|
||||||
|
if (pipe(pfds) < 0) return nullptr;
|
||||||
|
|
||||||
|
if (*type != 'r' && *type != 'w')
|
||||||
|
{
|
||||||
|
errno = EINVAL;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
pid_t child = fork();
|
||||||
|
if (child < 0)
|
||||||
|
{
|
||||||
|
close(pfds[0]);
|
||||||
|
close(pfds[1]);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
if (child == 0)
|
||||||
|
{
|
||||||
|
if (*type == 'r')
|
||||||
|
{
|
||||||
|
close(pfds[0]);
|
||||||
|
dup2(pfds[1], STDOUT_FILENO);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
close(pfds[1]);
|
||||||
|
dup2(pfds[0], STDIN_FILENO);
|
||||||
|
}
|
||||||
|
|
||||||
|
execl("/bin/sh", "sh", "-c", command, nullptr);
|
||||||
|
_exit(127);
|
||||||
|
}
|
||||||
|
|
||||||
|
int fd;
|
||||||
|
if (*type == 'r')
|
||||||
|
{
|
||||||
|
close(pfds[1]);
|
||||||
|
fd = pfds[0];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
close(pfds[0]);
|
||||||
|
fd = pfds[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
int err = errno;
|
||||||
|
FILE* f = (FILE*)malloc(sizeof(FILE));
|
||||||
|
if (!f)
|
||||||
|
{
|
||||||
|
errno = err;
|
||||||
|
close(fd);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
f->_fd = fd;
|
||||||
|
f->_pid = child;
|
||||||
|
clearerr(f);
|
||||||
|
|
||||||
|
f->_flags = *type == 'r' ? O_RDONLY : O_WRONLY;
|
||||||
|
f->_buf.status = 0;
|
||||||
|
f->_buf.mode = _IOFBF;
|
||||||
|
f->_buf.size = f->_buf.index = 0;
|
||||||
|
f->_buf.buffer = nullptr;
|
||||||
|
setvbuf(f, NULL, f->_buf.mode, 0);
|
||||||
|
|
||||||
|
s_open_files[fd] = f;
|
||||||
|
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
int pclose(FILE* stream)
|
||||||
|
{
|
||||||
|
pid_t pid = stream->_pid;
|
||||||
|
fclose(stream);
|
||||||
|
|
||||||
|
int status;
|
||||||
|
if (waitpid(pid, &status, 0) < 0) return -1;
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <luna/Types.h>
|
#include <luna/Types.h>
|
||||||
|
|
||||||
namespace CRC32
|
class CRC32
|
||||||
{
|
{
|
||||||
u32 checksum(const u8* data, usize size);
|
public:
|
||||||
}
|
void append(const u8* data, usize size);
|
||||||
|
|
||||||
|
u32 digest();
|
||||||
|
|
||||||
|
private:
|
||||||
|
u32 m_checksum = 0xffffffffu;
|
||||||
|
};
|
||||||
|
@ -33,18 +33,16 @@ static const u32 crc_table[] = {
|
|||||||
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
|
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace CRC32
|
void CRC32::append(const u8* data, usize size)
|
||||||
{
|
{
|
||||||
u32 checksum(const u8* data, usize size)
|
|
||||||
{
|
|
||||||
u32 crc = 0xffffffffu;
|
|
||||||
|
|
||||||
for (usize i = 0; i < size; i++)
|
for (usize i = 0; i < size; i++)
|
||||||
{
|
{
|
||||||
const u32 index = (crc & 0xff) ^ data[i];
|
const u32 index = (m_checksum & 0xff) ^ data[i];
|
||||||
crc = (crc >> 8) ^ (crc_table[index]);
|
m_checksum = (m_checksum >> 8) ^ (crc_table[index]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ~crc;
|
u32 CRC32::digest()
|
||||||
}
|
{
|
||||||
|
return ~m_checksum;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user