]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - VBLADE/vblade-master/linux.c
Continue to boot even the ISO file size is invalid.
[Ventoy.git] / VBLADE / vblade-master / linux.c
1 // linux.c: low level access routines for Linux
2 #define _GNU_SOURCE
3 #include "config.h"
4 #include <sys/socket.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <sys/time.h>
10 #include <features.h> /* for the glibc version number */
11 #if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
12 #include <netpacket/packet.h>
13 #include <net/ethernet.h> /* the L2 protocols */
14 #else
15 #include <asm/types.h>
16 #include <linux/if_packet.h>
17 #include <linux/if_ether.h> /* The L2 protocols */
18 #endif
19
20 #include <fcntl.h>
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
23 #include <net/if.h>
24 #include <netinet/in.h>
25 #include <linux/fs.h>
26 #include <sys/stat.h>
27
28 #include "dat.h"
29 #include "fns.h"
30
31 int getindx(int, char *);
32 int getea(int, char *, uchar *);
33
34
35
36 int
37 dial(char *eth, int bufcnt) // get us a raw connection to an interface
38 {
39 int i, n, s;
40 struct sockaddr_ll sa;
41 enum { aoe_type = 0x88a2 };
42
43 memset(&sa, 0, sizeof sa);
44 s = socket(PF_PACKET, SOCK_RAW, htons(aoe_type));
45 if (s == -1) {
46 perror("got bad socket");
47 return -1;
48 }
49 i = getindx(s, eth);
50 if (i < 0) {
51 perror(eth);
52 return -1;
53 }
54 sa.sll_family = AF_PACKET;
55 sa.sll_protocol = htons(0x88a2);
56 sa.sll_ifindex = i;
57 n = bind(s, (struct sockaddr *)&sa, sizeof sa);
58 if (n == -1) {
59 perror("bind funky");
60 return -1;
61 }
62
63 struct bpf_program {
64 ulong bf_len;
65 void *bf_insns;
66 } *bpf_program = create_bpf_program(shelf, slot);
67 setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, bpf_program, sizeof(*bpf_program));
68 free_bpf_program(bpf_program);
69
70 n = bufcnt * getmtu(s, eth);
71 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) < 0)
72 perror("setsockopt SOL_SOCKET, SO_SNDBUF");
73 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) < 0)
74 perror("setsockopt SOL_SOCKET, SO_RCVBUF");
75
76 return s;
77 }
78
79 int
80 getindx(int s, char *name) // return the index of device 'name'
81 {
82 struct ifreq xx;
83 int n;
84
85 snprintf(xx.ifr_name, sizeof xx.ifr_name, "%s", name);
86 n = ioctl(s, SIOCGIFINDEX, &xx);
87 if (n == -1)
88 return -1;
89 return xx.ifr_ifindex;
90 }
91
92 int
93 getea(int s, char *name, uchar *ea)
94 {
95 struct ifreq xx;
96 int n;
97
98 snprintf(xx.ifr_name, sizeof xx.ifr_name, "%s", name);
99 n = ioctl(s, SIOCGIFHWADDR, &xx);
100 if (n == -1) {
101 perror("Can't get hw addr");
102 return 0;
103 }
104 memmove(ea, xx.ifr_hwaddr.sa_data, 6);
105 return 1;
106 }
107
108 int
109 getmtu(int s, char *name)
110 {
111 struct ifreq xx;
112 int n;
113
114 snprintf(xx.ifr_name, sizeof xx.ifr_name, "%s", name);
115 n = ioctl(s, SIOCGIFMTU, &xx);
116 if (n == -1) {
117 perror("Can't get mtu");
118 return 1500;
119 }
120 return xx.ifr_mtu;
121 }
122
123 #if 0
124 int
125 getsec(int fd, uchar *place, vlong lba, int nsec)
126 {
127 return pread(fd, place, nsec * 512, lba * 512);
128 }
129
130 int
131 putsec(int fd, uchar *place, vlong lba, int nsec)
132 {
133 return pwrite(fd, place, nsec * 512, lba * 512);
134 }
135 #endif
136
137 int
138 getpkt(int fd, uchar *buf, int sz)
139 {
140 return read(fd, buf, sz);
141 }
142
143 int
144 putpkt(int fd, uchar *buf, int sz)
145 {
146 return write(fd, buf, sz);
147 }
148
149 vlong
150 getsize(int fd)
151 {
152 vlong size;
153 struct stat s;
154 int n;
155
156 n = ioctl(fd, BLKGETSIZE64, &size);
157 if (n == -1) { // must not be a block special
158 n = fstat(fd, &s);
159 if (n == -1) {
160 perror("getsize");
161 exit(1);
162 }
163 size = s.st_size;
164 }
165 return size;
166 }