xref: /plan9-contrib/sys/src/9/pcboot/fs.c (revision 25210b069a6ed8c047fa67220cf1dff32812f121)
1 #include	"u.h"
2 #include	"../port/lib.h"
3 #include	"mem.h"
4 #include	"dat.h"
5 #include	"fns.h"
6 #include	"io.h"
7 #include	"ureg.h"
8 #include	"pool.h"
9 #include	"../port/error.h"
10 #include	"../port/netif.h"
11 #include	"dosfs.h"
12 
13 enum {
14 	Bufsize = 8192,
15 };
16 
17 /*
18  *  grab next element from a path, return the pointer to unprocessed portion of
19  *  path.
20  */
21 char *
nextelem(char * path,char * elem)22 nextelem(char *path, char *elem)
23 {
24 	int i;
25 
26 	while(*path == '/')
27 		path++;
28 	if(*path==0 || *path==' ')
29 		return 0;
30 	for(i=0; *path!='\0' && *path!='/' && *path!=' '; i++){
31 		if(i==NAMELEN){
32 			print("name component too long\n");
33 			return 0;
34 		}
35 		*elem++ = *path++;
36 	}
37 	*elem = '\0';
38 	return path;
39 }
40 
41 int
fswalk(Bootfs * fs,char * path,File * f)42 fswalk(Bootfs *fs, char *path, File *f)
43 {
44 	char element[NAMELEN];
45 
46 	*f = fs->root;
47 	if(BADPTR(fs->walk))
48 		panic("fswalk bad pointer fs->walk");
49 
50 	f->path = path;
51 	while(path = nextelem(path, element)){
52 		switch(fs->walk(f, element)){
53 		case -1:
54 			return -1;
55 		case 0:
56 			return 0;
57 		}
58 	}
59 	return 1;
60 }
61 
62 /*
63  *  boot
64  */
65 int
fsboot(Bootfs * fs,char * path,Boot * b)66 fsboot(Bootfs *fs, char *path, Boot *b)
67 {
68 	long n;
69 	char *buf;
70 	File file;
71 
72 	memset(&file, 0, sizeof file);
73 	switch(fswalk(fs, path, &file)){
74 	case -1:
75 		print("error walking to %s\n", path);
76 		return -1;
77 	case 0:
78 		print("%s not found\n", path);
79 		return -1;
80 	case 1:
81 		print("found %s\n", path);
82 		break;
83 	}
84 	buf = smalloc(Bufsize);
85 	while((n = fsread(&file, buf, Bufsize)) > 0) {
86 		if(bootpass(b, buf, n) != MORE)
87 			break;
88 	}
89 
90 	bootpass(b, nil, 0);	/* tries boot */
91 	free(buf);
92 	return -1;
93 }
94 
95 int
fsread(File * file,void * a,long n)96 fsread(File *file, void *a, long n)
97 {
98 	if(BADPTR(file->fs))
99 		panic("bad pointer file->fs in fsread");
100 	if(BADPTR(file->fs->read))
101 		panic("bad pointer file->fs->read in fsread");
102 	return file->fs->read(file, a, n);
103 }
104