summaryrefslogtreecommitdiff
path: root/src/kernel/files.c
blob: b8a90611a1a15a83a350c65d87775eb547ba433b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
Copyright 2019 Benji Dial

Permission to use, copy, modify, and/or distribute this
software for any purpose with or without fee is hereby
granted, provided that the above copyright notice and this
permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS
ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
OF THIS SOFTWARE.
*/

#include "files.h"
#include "diskio.h"
#include "gpt.h"

uint8_t working_drive;
uint8_t working_part;
uint8_t buffer[1024];

void set_working_drive(uint8_t name, uint8_t part) {
  if ((name >= (uint8_t)'a') && (name <= (uint8_t)'z') &&
    (drives[name - (uint8_t)'a'].flags & PRESENT))
    working_drive = name - (uint8_t)'a';
  if (part_info[working_drive].format & ~DIRECT)
    working_part =
      (part >= (uint8_t)'0') && (name <= (uint8_t)'9') &&
      (part_info[working_drive].n_partitions > part) ?
      (part & 0x0f) : 0;
}

uint16_t open_file(uint8_t *name) {
  return 0;//TODO
}

void close_file(uint16_t handle) {
  file_table[handle].first_sector = 0;
}

uint32_t read_file(uint16_t handle, uint32_t length, void *buffer) {
  //TODO
}

void write_file(uint16_t handle, uint32_t length, void *buffer) {
  //TODO
}

void seek_file(uint16_t handle, int32_t by) {
  //TODO
}

uint32_t get_size(uint16_t handle) {
  return 0;//TODO
}

uint32_t read_line_file(uint16_t handle, uint32_t max, void *buffer) {
  uint8_t *i = (uint8_t *)buffer;
  while (i < (uint8_t *)buffer + max) {
    if (!read_file(handle, 1, i) || (*i == (uint8_t)'\n')) {
      *i = 0;
      return i - (uint8_t *)buffer;
    }
    ++i;
  }
}

void detect_disk_parts(uint8_t drive) {
  if ((drive >= (uint8_t)'a') && (drive <= (uint8_t)'z')) {
    uint8_t dn = (drive & 0x1f) - 1;
    update_status(dn);
    if (drives[dn].flags & (PRESENT | DISK_IN)) {
      read_sectors(dn, 0, 2, buffer);
      if ((*(uint32_t *)&buffer[0x200] == 0x20494645) &&//EFI
          (*(uint32_t *)&buffer[0x204] == 0x54524150))  //PART
        part_info[dn].format = gpt_parse_pt(dn, buffer) ? GPT : UNKNOWN;
      ;
    }
    else {
      part_info[dn].format = NOT_PRESENT;
      part_info[dn].n_partitions = 0;
    }
  }
}