static int setfs(struct sockaddr_in *addr, char *path, char *p) { unsigned ip = 0; int val; if (((val = getdec(&p)) < 0) || (val > 255)) return(0); ip = val << 24; if (*p != '.') return(0); p++; if (((val = getdec(&p)) < 0) || (val > 255)) return(0); ip |= (val << 16); if (*p != '.') return(0); p++; if (((val = getdec(&p)) < 0) || (val > 255)) return(0); ip |= (val << 8); if (*p != '.') return(0); p++; if (((val = getdec(&p)) < 0) || (val > 255)) return(0); ip |= val; if (*p != ':') return(0); p++; addr->sin_addr.s_addr = htonl(ip); addr->sin_len = sizeof(struct sockaddr_in); addr->sin_family = AF_INET; strncpy(path,p,MNAMELEN-1); return(1); }
static int getip(char **ptr, struct in_addr *addr) { char *p; unsigned int ip; int val; p = *ptr; ip = 0; if (((val = getdec(&p)) < 0) || (val > 255)) return 0; ip = val << 24; if (*p != '.') return 0; p++; if (((val = getdec(&p)) < 0) || (val > 255)) return 0; ip |= (val << 16); if (*p != '.') return 0; p++; if (((val = getdec(&p)) < 0) || (val > 255)) return 0; ip |= (val << 8); if (*p != '.') return 0; p++; if (((val = getdec(&p)) < 0) || (val > 255)) return 0; ip |= val; addr->s_addr = htonl(ip); *ptr = p; return 1; }
void nfs_mountopts(struct nfs_args *args, char *p) { char *tmp; args->version = NFS_ARGSVERSION; args->rsize = 8192; args->wsize = 8192; args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT; args->sotype = SOCK_STREAM; if (p == NULL) return; if ((tmp = (char *)substr(p, "rsize="))) args->rsize = getdec(&tmp); if ((tmp = (char *)substr(p, "wsize="))) args->wsize = getdec(&tmp); if ((tmp = (char *)substr(p, "intr"))) args->flags |= NFSMNT_INT; if ((tmp = (char *)substr(p, "soft"))) args->flags |= NFSMNT_SOFT; if ((tmp = (char *)substr(p, "noconn"))) args->flags |= NFSMNT_NOCONN; if ((tmp = (char *)substr(p, "udp"))) args->sotype = SOCK_DGRAM; }
void testThread(void* pid) { int id = (int)pid; while (1) { printf("\nIn cog %d, enter a decimal (enter %d to exit): ", id, id); int d = getdec(); printf("From cog %d, you entered: %d\n", id, d); if (d == id) { printf("\nCog %d exiting. Bye!\n", id); exit(0); } } }
static int tftp (const char *name, int (*fnc) (unsigned char *, int, int, int)) { int retry = 0; static unsigned short iport = 2000; unsigned short oport = 0; unsigned short len, block = 0, prevblock = 0; int bcounter = 0; struct tftp_t *tr; struct tftpreq_t tp; int rc; int packetsize = TFTP_DEFAULTSIZE_PACKET; await_reply (AWAIT_QDRAIN, 0, NULL, 0); tp.opcode = htons (TFTP_RRQ); len = (grub_sprintf ((char *) tp.u.rrq, "%s%coctet%cblksize%c%d", name, 0, 0, 0, TFTP_MAX_PACKET) + sizeof (tp.ip) + sizeof (tp.udp) + sizeof (tp.opcode) + 1); if (! udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr, ++iport, TFTP_PORT, len, &tp)) return 0; for (;;) { long timeout; #ifdef CONGESTED timeout = rfc2131_sleep_interval (block ? TFTP_REXMT : TIMEOUT, retry); #else timeout = rfc2131_sleep_interval (TIMEOUT, retry); #endif if (! await_reply (AWAIT_TFTP, iport, NULL, timeout)) { if (! block && retry++ < MAX_TFTP_RETRIES) { if (! udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr, ++iport, TFTP_PORT, len, &tp)) return 0; continue; } #ifdef CONGESTED if (block && ((retry += TFTP_REXMT) < TFTP_TIMEOUT)) { #ifdef MDEBUG grub_printf ("<REXMT>\n"); #endif udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr, iport, oport, TFTP_MIN_PACKET, &tp); continue; } #endif break; } tr = (struct tftp_t *) &nic.packet[ETH_HLEN]; if (tr->opcode == ntohs (TFTP_ERROR)) { grub_printf ("TFTP error %d (%s)\n", ntohs (tr->u.err.errcode), tr->u.err.errmsg); break; } if (tr->opcode == ntohs (TFTP_OACK)) { char *p = tr->u.oack.data, *e; if (prevblock) continue; len = ntohs (tr->udp.len) - sizeof (struct udphdr) - 2; if (len > TFTP_MAX_PACKET) goto noak; e = p + len; while (*p != '\000' && p < e) { if (! grub_strcmp ("blksize", p)) { p += 8; if ((packetsize = getdec (&p)) < TFTP_DEFAULTSIZE_PACKET) goto noak; while (p < e && *p) p++; if (p < e) p++; } else { noak: tp.opcode = htons (TFTP_ERROR); tp.u.err.errcode = 8; len = (grub_sprintf ((char *) tp.u.err.errmsg, "RFC1782 error") + sizeof (tp.ip) + sizeof (tp.udp) + sizeof (tp.opcode) + sizeof (tp.u.err.errcode) + 1); udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr, iport, ntohs (tr->udp.src), len, &tp); return 0; } } if (p > e) goto noak; block = tp.u.ack.block = 0; } else if (tr->opcode == ntohs (TFTP_DATA)) { len = ntohs (tr->udp.len) - sizeof (struct udphdr) - 4; if (len > packetsize) continue; block = ntohs (tp.u.ack.block = tr->u.data.block); } else break; if ((block || bcounter) && (block != prevblock + 1)) tp.u.ack.block = htons (block = prevblock); tp.opcode = htons (TFTP_ACK); oport = ntohs (tr->udp.src); udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr, iport, oport, TFTP_MIN_PACKET, &tp); if ((unsigned short) (block - prevblock) != 1) continue; prevblock = block; retry = 0; if ((rc = fnc (tr->u.data.download, ++bcounter, len, len < packetsize)) >= 0) return rc; if (len < packetsize) return 1; } return 0; }
/* Fill the buffer by receiving the data via the TFTP protocol. */ static int buf_fill (int abort) { #ifdef TFTP_DEBUG grub_printf ("buf_fill (%d)\n", abort); #endif while (! buf_eof && (buf_read + packetsize <= FSYS_BUFLEN)) { struct tftp_t *tr; long timeout; #ifdef CONGESTED timeout = rfc2131_sleep_interval (block ? TFTP_REXMT : TIMEOUT, retry); #else timeout = rfc2131_sleep_interval (TIMEOUT, retry); #endif if (! await_reply (AWAIT_TFTP, iport, NULL, timeout)) { if (ip_abort) return 0; if (! block && retry++ < MAX_TFTP_RETRIES) { /* Maybe initial request was lost. */ #ifdef TFTP_DEBUG grub_printf ("Maybe initial request was lost.\n"); #endif if (! udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr, ++iport, TFTP_PORT, len, &tp)) return 0; continue; } #ifdef CONGESTED if (block && ((retry += TFTP_REXMT) < TFTP_TIMEOUT)) { /* We resend our last ack. */ # ifdef TFTP_DEBUG grub_printf ("<REXMT>\n"); # endif udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr, iport, oport, TFTP_MIN_PACKET, &tp); continue; } #endif /* Timeout. */ return 0; } tr = (struct tftp_t *) &nic.packet[ETH_HLEN]; if (tr->opcode == ntohs (TFTP_ERROR)) { grub_printf ("TFTP error %d (%s)\n", ntohs (tr->u.err.errcode), tr->u.err.errmsg); return 0; } if (tr->opcode == ntohs (TFTP_OACK)) { char *p = tr->u.oack.data, *e; #ifdef TFTP_DEBUG grub_printf ("OACK "); #endif /* Shouldn't happen. */ if (prevblock) { /* Ignore it. */ grub_printf ("%s:%d: warning: PREVBLOCK != 0 (0x%x)\n", __FILE__, __LINE__, prevblock); continue; } len = ntohs (tr->udp.len) - sizeof (struct udphdr) - 2; if (len > TFTP_MAX_PACKET) goto noak; e = p + len; while (*p != '\000' && p < e) { if (! grub_strcmp ("blksize", p)) { p += 8; if ((packetsize = getdec (&p)) < TFTP_DEFAULTSIZE_PACKET) goto noak; #ifdef TFTP_DEBUG grub_printf ("blksize = %d\n", packetsize); #endif } else if (! grub_strcmp ("tsize", p)) { p += 6; if ((filemax = getdec (&p)) < 0) { filemax = -1; goto noak; } #ifdef TFTP_DEBUG grub_printf ("tsize = %d\n", filemax); #endif } else { noak: #ifdef TFTP_DEBUG grub_printf ("NOAK\n"); #endif tp.opcode = htons (TFTP_ERROR); tp.u.err.errcode = 8; len = (grub_sprintf ((char *) tp.u.err.errmsg, "RFC1782 error") + sizeof (tp.ip) + sizeof (tp.udp) + sizeof (tp.opcode) + sizeof (tp.u.err.errcode) + 1); udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr, iport, ntohs (tr->udp.src), len, &tp); return 0; } while (p < e && *p) p++; if (p < e) p++; } if (p > e) goto noak; /* This ensures that the packet does not get processed as data! */ block = tp.u.ack.block = 0; } else if (tr->opcode == ntohs (TFTP_DATA)) { #ifdef TFTP_DEBUG grub_printf ("DATA "); #endif len = ntohs (tr->udp.len) - sizeof (struct udphdr) - 4; /* Shouldn't happen. */ if (len > packetsize) { /* Ignore it. */ grub_printf ("%s:%d: warning: LEN > PACKETSIZE (0x%x > 0x%x)\n", __FILE__, __LINE__, len, packetsize); continue; } block = ntohs (tp.u.ack.block = tr->u.data.block); } else /* Neither TFTP_OACK nor TFTP_DATA. */ break; if ((block || bcounter) && (block != prevblock + (unsigned short) 1)) /* Block order should be continuous */ tp.u.ack.block = htons (block = prevblock); /* Should be continuous. */ tp.opcode = abort ? htons (TFTP_ERROR) : htons (TFTP_ACK); oport = ntohs (tr->udp.src); #ifdef TFTP_DEBUG grub_printf ("ACK\n"); #endif /* Ack. */ udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr, iport, oport, TFTP_MIN_PACKET, &tp); if (abort) { buf_eof = 1; break; } /* Retransmission or OACK. */ if ((unsigned short) (block - prevblock) != 1) /* Don't process. */ continue; prevblock = block; /* Is it the right place to zero the timer? */ retry = 0; /* In GRUB, this variable doesn't play any important role at all, but use it for consistency with Etherboot. */ bcounter++; /* Copy the downloaded data to the buffer. */ grub_memmove (buf + buf_read, tr->u.data.download, len); buf_read += len; /* End of data. */ if (len < packetsize) buf_eof = 1; } return 1; }
int main() { printf("This works %d%%\n", 100); printf("Hello World!\n"); printf("Norm nums: %d %u %x %c %s%s", -1, -2, 0xabcdefab, 'X', "x", "\n"); // Note: these don't work with __simple_printf! printf("BFill nums: %4d %12u %10x %3c %3s%s", -1, -2, 0xabcdefab, 'X', "x", "\n"); printf("LFill nums: %04d %012u %010x %3c %3s%s", -1, -2, 0xabcdefab, 'X', "x", "\n"); #pragma GCC diagnostic ignored "-Wformat" // The underlying library actively ignores the zeros -- we want to test it! printf("RFill nums: %-04d %-012u %-010x %-3c %-3s%s", -1, -2, 0xabcdefab, 'X', "x", "\n"); // The underlying library detects and handles this error - we want to test it! printf("%"); #pragma GCC diagnostic warning "-Wformat" #ifdef __TINY_IO_H putstr("Put* nums: "); putdec(-1); putchar(' '); putudec(-1); putchar(' '); puthex(0xabcdefab); putchar(' '); putchar('X'); putchar(' '); putstr("x"); putchar(' '); putfnum(0xabcdefab, 16, 0, 10, '0'); putchar('\n'); putlhex(-10000000000L); putchar(' '); putldec(-10000000000L); putchar(' '); putlfnum(0xabcdefabcdef, 16, 0, 16, '0'); putchar('\n'); #endif char buf[80]; sprintf(buf, "Norm nums: %d %u %x %c %s", -1, -2, 0xabcdefab, 'X', "x"); puts(buf); int d; unsigned u, x; char c; char s[20]; int n = sscanf(buf, "Norm nums: %d %u %x %c %s\n", &d, &u, &x, &c, s); printf("Scan nums: %d %u %x %c %s (%d scanned)\n", d, u, x, c, s, n); printf("\nGimme a character: "); char ch = getchar(); printf("\nYou typed: "); putchar(ch); putchar('\n'); int age; do { printf("\nHow old are you? "); scanf("%u", &age); } while (!age); printf("In ten years, you'll be: %d\n\n", age + 10); sprintf(buf, "BFill nums: %4d %12u %10x %3c %3s", -1, -2, 0xabcdefab, 'X', "x"); puts(buf); n = sscanf(buf, "BFill nums: %4d %12u %10x %3c %3s\n",&d, &u, &x, &c, s); printf("Scan nums: %d %u %x %c %s (%d scanned)\n", d, u, x, c, s, n); sprintf(buf, "LFill nums: %04d %012u %010x %3c %3s", -1, -2, 0xabcdefab, 'X', "x"); puts(buf); n = sscanf(buf, "LFill nums: %04d %012u %010x %3c %3s\n",&d, &u, &x, &c, s); printf("Scan nums: %d %u %x %c %s (%d scanned)\n", d, u, x, c, s, n); #ifdef __TINY_IO_H printf("\nEnter a string up to 4 characters: "); safe_gets(s, 5); printf("You entered: %s\n", s); printf("\nEnter a decimal: "); d = getdec(); printf("You entered: %d\n", d); printf("\nEnter an unsigned decimal: "); u = getudec(); printf("You entered: %d\n", u); printf("\nEnter a hex number: "); u = gethex(); printf("You entered: %x\n", u); printf("\nEnter a 1-4 digit number: "); d = getfnum(10, 1, 4); printf("You entered: %d\n", (int)d); long long ld; unsigned long long lu, lx; printf("\nEnter a long long decimal: "); ld = getldec(); printf("You entered: "); putldec(ld); putchar('\n'); printf("\nEnter an unsigned long long decimal: "); lu = getludec(); printf("You entered: "); putludec(lu); putchar('\n'); printf("\nEnter a long long hex number: "); lx = getlhex(); printf("You entered: "); putlhex(lx); putchar('\n'); printf("\nEnter a 1-12 digit long long number: "); ld = getlfnum(10, 1, 12); printf("You entered: "); putldec(ld); putchar('\n'); #endif _serialLock = 1; printf("\n\nMultithreaded tests.\n" "Note this will be quite messed up because multiple cogs\n" "will be doing I/O on a character-by-character basis...\n"); _start_cog_thread(threadStack(0), &testThread, (void*)1, &tls[0]); _start_cog_thread(threadStack(1), &testThread, (void*)2, &tls[1]); testThread(0); printf("\nBye!\n"); return 0; }
static int tftp (const char *name, int (*fnc) (unsigned char *, int, int, int)) { int retry = 0; static unsigned short iport = 2000; unsigned short oport = 0; unsigned short len, block = 0, prevblock = 0; int bcounter = 0; struct tftp_t *tr; struct tftpreq_t tp; int rc; int packetsize = TFTP_DEFAULTSIZE_PACKET; /* Clear out the Rx queue first. It contains nothing of interest, * except possibly ARP requests from the DHCP/TFTP server. We use * polling throughout Etherboot, so some time may have passed since we * last polled the receive queue, which may now be filled with * broadcast packets. This will cause the reply to the packets we are * about to send to be lost immediately. Not very clever. */ await_reply (AWAIT_QDRAIN, 0, NULL, 0); tp.opcode = htons (TFTP_RRQ); len = (grub_sprintf ((char *) tp.u.rrq, "%s%coctet%cblksize%c%d", name, 0, 0, 0, TFTP_MAX_PACKET) + sizeof (tp.ip) + sizeof (tp.udp) + sizeof (tp.opcode) + 1); if (! udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr, ++iport, TFTP_PORT, len, &tp)) return 0; for (;;) { long timeout; #ifdef CONGESTED timeout = rfc2131_sleep_interval (block ? TFTP_REXMT : TIMEOUT, retry); #else timeout = rfc2131_sleep_interval (TIMEOUT, retry); #endif if (! await_reply (AWAIT_TFTP, iport, NULL, timeout)) { if (! block && retry++ < MAX_TFTP_RETRIES) { /* Maybe initial request was lost. */ if (! udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr, ++iport, TFTP_PORT, len, &tp)) return 0; continue; } #ifdef CONGESTED if (block && ((retry += TFTP_REXMT) < TFTP_TIMEOUT)) { /* We resend our last ack. */ #ifdef MDEBUG grub_printf ("<REXMT>\n"); #endif udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr, iport, oport, TFTP_MIN_PACKET, &tp); continue; } #endif /* Timeout. */ break; } tr = (struct tftp_t *) &nic.packet[ETH_HLEN]; if (tr->opcode == ntohs (TFTP_ERROR)) { grub_printf ("TFTP error %d (%s)\n", ntohs (tr->u.err.errcode), tr->u.err.errmsg); break; } if (tr->opcode == ntohs (TFTP_OACK)) { char *p = tr->u.oack.data, *e; /* Shouldn't happen. */ if (prevblock) /* Ignore it. */ continue; len = ntohs (tr->udp.len) - sizeof (struct udphdr) - 2; if (len > TFTP_MAX_PACKET) goto noak; e = p + len; while (*p != '\000' && p < e) { if (! grub_strcmp ("blksize", p)) { p += 8; if ((packetsize = getdec (&p)) < TFTP_DEFAULTSIZE_PACKET) goto noak; while (p < e && *p) p++; if (p < e) p++; } else { noak: tp.opcode = htons (TFTP_ERROR); tp.u.err.errcode = 8; len = (grub_sprintf ((char *) tp.u.err.errmsg, "RFC1782 error") + sizeof (tp.ip) + sizeof (tp.udp) + sizeof (tp.opcode) + sizeof (tp.u.err.errcode) + 1); udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr, iport, ntohs (tr->udp.src), len, &tp); return 0; } } if (p > e) goto noak; /* This ensures that the packet does not get processed as data! */ block = tp.u.ack.block = 0; } else if (tr->opcode == ntohs (TFTP_DATA)) { len = ntohs (tr->udp.len) - sizeof (struct udphdr) - 4; /* Shouldn't happen. */ if (len > packetsize) /* Ignore it. */ continue; block = ntohs (tp.u.ack.block = tr->u.data.block); } else /* Neither TFTP_OACK nor TFTP_DATA. */ break; if ((block || bcounter) && (block != prevblock + 1)) /* Block order should be continuous */ tp.u.ack.block = htons (block = prevblock); /* Should be continuous. */ tp.opcode = htons (TFTP_ACK); oport = ntohs (tr->udp.src); /* Ack. */ udp_transmit (arptable[ARP_SERVER].ipaddr.s_addr, iport, oport, TFTP_MIN_PACKET, &tp); if ((unsigned short) (block - prevblock) != 1) /* Retransmission or OACK, don't process via callback * and don't change the value of prevblock. */ continue; prevblock = block; /* Is it the right place to zero the timer? */ retry = 0; if ((rc = fnc (tr->u.data.download, ++bcounter, len, len < packetsize)) >= 0) return rc; /* End of data. */ if (len < packetsize) return 1; } return 0; }
void dcprint(Blk *hptr) { Blk *p, *q, *dec; int dig, dout, ct, sc; rewind(hptr); while(sfeof(hptr) == 0) { if(sgetc(hptr)>99) { rewind(hptr); while(sfeof(hptr) == 0) { Bprint(&bout,"%c",sgetc(hptr)); } Bprint(&bout,"\n"); return; } } fsfile(hptr); sc = sbackc(hptr); if(sfbeg(hptr) != 0) { Bprint(&bout,"0\n"); return; } count = ll; p = copy(hptr,length(hptr)); sclobber(p); fsfile(p); if(sbackc(p)<0) { chsign(p); OUTC('-'); } if((obase == 0) || (obase == -1)) { oneot(p,sc,'d'); return; } if(obase == 1) { oneot(p,sc,'1'); return; } if(obase == 10) { tenot(p,sc); return; } /* sleazy hack to scale top of stack - divide by 1 */ pushp(p); sputc(p, sc); p=salloc(0); create(p); sputc(p, 1); sputc(p, 0); pushp(p); if(dscale() != 0) return; p = div(arg1, arg2); release(arg1); release(arg2); sc = savk; create(strptr); dig = logten*sc; dout = ((dig/10) + dig) / logo; dec = getdec(p,sc); p = removc(p,sc); while(length(p) != 0) { q = div(p,basptr); release(p); p = q; (*outdit)(rem,0); } release(p); fsfile(strptr); while(sfbeg(strptr) == 0) OUTC(sbackc(strptr)); if(sc == 0) { release(dec); Bprint(&bout,"\n"); return; } create(strptr); OUTC('.'); ct=0; do { q = mult(basptr,dec); release(dec); dec = getdec(q,sc); p = removc(q,sc); (*outdit)(p,1); } while(++ct < dout); release(dec); rewind(strptr); while(sfeof(strptr) == 0) OUTC(sgetc(strptr)); Bprint(&bout,"\n"); }
void print(struct blk *hptr) { int sc; register struct blk *p,*q,*dec; int dig,dout,ct; rewind(hptr); while(sfeof(hptr) == 0){ if(sgetc(hptr)>99){ rewind(hptr); while(sfeof(hptr) == 0){ printf("%c",sgetc(hptr)); } printf("\n"); return; } } fsfile(hptr); sc = sbackc(hptr); if(sfbeg(hptr) != 0){ printf("0\n"); return; } count = ll; p = copy(hptr,length(hptr)); sunputc(p); fsfile(p); if(sbackc(p)<0){ chsign(p); OUTC('-'); } if((obase == 0) || (obase == -1)){ oneot(p,sc,'d'); return; } if(obase == 1){ oneot(p,sc,'1'); return; } if(obase == 10){ tenot(p,sc); return; } create(strptr); dig = log_10*sc; dout = ((dig/10) + dig) /logo; dec = getdec(p,sc); p = removc(p,sc); while(length(p) != 0){ q = dcdiv(p,basptr); release(p); p = q; (*outdit)(rem,0,1); } release(p); fsfile(strptr); while(sfbeg(strptr) == 0)OUTC(sbackc(strptr)); if(sc == 0){ release(dec); printf("\n"); return; } create(strptr); OUTC('.'); ct=0; do{ q = mult(basptr,dec); release(dec); dec = getdec(q,sc); p = removc(q,sc); (*outdit)(p,1,ct+1<dout); }while(++ct < dout); release(dec); rewind(strptr); while(sfeof(strptr) == 0)OUTC(sgetc(strptr)); printf("\n"); return; }