summaryrefslogtreecommitdiff
path: root/src/kernel/files.c
blob: 974768805dc45d5d06d88d17d4e1f11c9f0af279 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
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 "iso9660.h"
#include "mem.h"

bool (*const parse_pts[N_PT_FORMATS])(uint8_t dn, uint8_t *buffer) = {
  //&gpt_parse_pt,
  //&mbr_parse_pt
};

bool (*const parse_phs[N_FS_FORMATS])(uint8_t dn, uint8_t pn, uint32_t sector, uint8_t *buffer) = {
  &iso_9660_parse_ph
};

uint8_t working_drive;
uint8_t working_part;
uint8_t buffer[2048];

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;
    for (uint8_t i = 0; i < part_info[dn].n_partitions; ++i)
      if (part_info[dn].partition_cache[i])
        deallocate_block(part_info[dn].partition_cache[i]);
    update_status(dn);
    if (drives[dn].flags & (PRESENT | DISK_IN)) {
      /*ISO 9660 often comes with a dummy MBR*/
      if (iso_9660_parse_ph(dn, 0, 0, buffer)) {
        part_info[dn].format = DIRECT;
        part_info[dn].n_partitions = 1;
        return;
      }
      for (uint8_t ptt = 0; ptt < N_PT_FORMATS; ++ptt)
        if (parse_pts[ptt](dn, buffer))
          return;
      for (uint8_t fst = 0; fst < N_FS_FORMATS; ++fst)
        if (parse_phs[fst](dn, 0, 0, buffer)) {
          part_info[dn].format = DIRECT;
          part_info[dn].n_partitions = 1;
          return;
        }
      part_info[dn].format = UNKNOWN;
      part_info[dn].n_partitions = 0;
    }
    else {
      part_info[dn].format = NOT_PRESENT;
      part_info[dn].n_partitions = 0;
    }
  }
}