summaryrefslogtreecommitdiffstats
path: root/qemu/roms/openbios/fs/iso9660/iso9660_mount.c
blob: a58170408f0389496c809ff4c1dc5c6e1bba9811 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 *
 * (c) 2005-2009 Laurent Vivier <Laurent@vivier.eu>
 *
 * This file has been copied from EMILE, http://emile.sf.net
 *
 * some parts from mkisofs (c) J. Schilling
 *
 */

#include "libiso9660.h"
#include "libopenbios/bindings.h"
#include "libc/diskio.h"

void iso9660_name(iso9660_VOLUME *volume, struct iso_directory_record *idr, char *buffer)
{
	int	j;
        unsigned char ul, uc;

	buffer[0] = 0;
	if (idr->name_len[0] == 1 && idr->name[0] == 0)
		strcpy(buffer, ".");
	else if (idr->name_len[0] == 1 && idr->name[0] == 1)
		strcpy(buffer, "..");
	else {
		switch (volume->ucs_level) {
		case 3:
		case 2:
		case 1:
			/*
			 * Unicode name.
			 */

			for (j = 0; j < (int)idr->name_len[0] / 2; j++) {
				ul = idr->name[j*2+1];

				/*
				 * unicode convertion
				 * up = unls->unls_uni2cs[uh];
				 *
				 * if (up == NULL)
				 *	uc = '\0';
				 * else
				 *	uc = up[ul];
				 *
				 * we use only low byte
				 */

				uc = ul;

				buffer[j] = uc ? uc : '_';
			}
			buffer[idr->name_len[0]/2] = '\0';
			break;
		case 0:
			/*
			 * Normal non-Unicode name.
			 */
			strncpy(buffer, idr->name, idr->name_len[0]);
			buffer[idr->name_len[0]] = 0;
			break;
		default:
			/*
			 * Don't know how to do these yet.  Maybe they are the same
			 * as one of the above.
			 */
			break;
		}
	}
}

iso9660_VOLUME *iso9660_mount(int fd)
{
	iso9660_VOLUME* volume;
	struct iso_primary_descriptor *jpd;
	struct iso_primary_descriptor ipd;
	int	block;
	int ucs_level = 0;

	/* read filesystem descriptor */

	seek_io(fd, 16 * ISOFS_BLOCK_SIZE);
	read_io(fd, &ipd, sizeof (ipd));

	/*
	 * High sierra:
	 *
	 *	DESC TYPE	== 1 (VD_SFS)	offset 8	len 1
	 *	STR ID		== "CDROM"	offset 9	len 5
	 *	STD_VER		== 1		offset 14	len 1
	 */

	/* High Sierra format ? */

	if ((((char *)&ipd)[8] == 1) &&
	    (strncmp(&((char *)&ipd)[9], "CDROM", 5) == 0) &&
	    (((char *)&ipd)[14] == 1)) {
		printk("Incompatible format: High Sierra format\n");
		return NULL;
	}

	/*
	 * ISO 9660:
	 *
	 *	DESC TYPE	== 1 (VD_PVD)	offset 0	len 1
	 *	STR ID		== "CD001"	offset 1	len 5
	 *	STD_VER		== 1		offset 6	len 1
	 */

	/* NOT ISO 9660 format ? */

	if ((ipd.type[0] != ISO_VD_PRIMARY) ||
	    (strncmp(ipd.id, ISO_STANDARD_ID, sizeof (ipd.id)) != 0) ||
	    (ipd.version[0] != 1)) {
		return NULL;
	}

	/* UCS info */

	block = 16;

	jpd = (struct iso_primary_descriptor *)
		malloc(sizeof(struct iso_primary_descriptor));
	if (jpd == NULL)
		return NULL;

	memcpy(jpd, &ipd, sizeof (ipd));
	while ((uint8_t)jpd->type[0] != ISO_VD_END) {

		/*
		 * If Joliet UCS escape sequence found, we may be wrong
		 */

		if (jpd->unused3[0] == '%' &&
		    jpd->unused3[1] == '/' &&
		    (jpd->unused3[3] == '\0' ||
		    jpd->unused3[3] == ' ') &&
		    (jpd->unused3[2] == '@' ||
		    jpd->unused3[2] == 'C' ||
		    jpd->unused3[2] == 'E')) {

			if (jpd->version[0] != 1)
				break;
		}

		block++;
		seek_io(fd, block * ISOFS_BLOCK_SIZE);
		read_io(fd, jpd, sizeof (*jpd));
	}

	ucs_level = 0;
	if (((unsigned char) jpd->type[0] == ISO_VD_END)) {
		memcpy(jpd, &ipd, sizeof (ipd));
	} else {
		switch (jpd->unused3[2]) {
		case '@':
			ucs_level = 1;
			break;
		case 'C':
			ucs_level = 2;
			break;
		case 'E':
			ucs_level = 3;
			break;
		}

		if (ucs_level && jpd->unused3[3] == ' ')
			printk("Warning: Joliet escape sequence uses illegal space at offset 3\n");
	}

	volume = (iso9660_VOLUME*)malloc(sizeof(iso9660_VOLUME));
	if (volume == NULL)
		return NULL;

	volume->descriptor = jpd;
	volume->ucs_level = ucs_level;
	volume->fd = fd;

	return volume;
}

int iso9660_umount(iso9660_VOLUME* volume)
{
	if (volume == NULL)
		return -1;
	free(volume->descriptor);
	free(volume);
	return 0;
}

int iso9660_probe(int fd, long long offset)
{
	struct iso_primary_descriptor ipd;

	seek_io(fd, 16 * ISOFS_BLOCK_SIZE + offset);
	read_io(fd, &ipd, sizeof (ipd));

	if ((ipd.type[0] != ISO_VD_PRIMARY) ||
	    (strncmp(ipd.id, ISO_STANDARD_ID, sizeof (ipd.id)) != 0) ||
	    (ipd.version[0] != 1)) {
		return 0;
	}

	return -1;
}

struct iso_directory_record *iso9660_get_root_node(iso9660_VOLUME* volume)
{
	return (struct iso_directory_record *)volume->descriptor->root_directory_record;
}