static void *am33xx_net_boot(void) { void *buf = NULL; int err; int len; struct dhcp_req_param dhcp_param; const char *bootfile, *ip; char *file; am33xx_register_ethaddr(0, 0); memset(&dhcp_param, 0, sizeof(struct dhcp_req_param)); dhcp_param.vendor_id = "am335x barebox-mlo"; err = dhcp(20, &dhcp_param); if (err) { printf("dhcp failed\n"); return NULL; } /* * Older tftp server don't send the file size. * Then tftpfs needs temporary place to store the file. */ err = mount("none", "ramfs", "/", NULL); if (err < 0) { printf("failed to mount ramfs\n"); return NULL; } err = make_directory(TFTP_MOUNT); if (err) return NULL; ip = ip_to_string(net_get_serverip()); err = mount(ip, "tftp", TFTP_MOUNT, NULL); if (err < 0) { printf("Unable to mount.\n"); return NULL; } bootfile = getenv("bootfile"); if (!bootfile) { printf("bootfile not found.\n"); return NULL; } file = asprintf("%s/%s", TFTP_MOUNT, bootfile); buf = read_file(file, &len); if (!buf) printf("could not read %s.\n", bootfile); free(file); umount(TFTP_MOUNT); return buf; }
static int do_nfs(int argc, char *argv[]) { char *localfile; char *remotefile; if (argc < 2) return COMMAND_ERROR_USAGE; remotefile = argv[1]; if (argc == 2) localfile = basename(remotefile); else localfile = argv[2]; net_store_fd = open(localfile, O_WRONLY | O_CREAT); if (net_store_fd < 0) { perror("open"); return 1; } nfs_con = net_udp_new(net_get_serverip(), 0, nfs_handler, NULL); if (IS_ERR(nfs_con)) { nfs_err = PTR_ERR(nfs_con); goto err_udp; } net_udp_bind(nfs_con, 1000); nfs_err = 0; nfs_start(remotefile); while (nfs_state != STATE_DONE) { if (ctrlc()) { nfs_err = -EINTR; break; } net_poll(); if (is_timeout(nfs_timer_start, NFS_TIMEOUT * SECOND)) { show_progress(-1); nfs_send(); } } net_unregister(nfs_con); err_udp: close(net_store_fd); if (nfs_err) { printf("NFS failed: %s\n", strerror(-nfs_err)); unlink(localfile); } printf("\n"); return nfs_err == 0 ? 0 : 1; }
static void *am33xx_net_boot(void) { void *buf = NULL; int err; int len; struct dhcp_req_param dhcp_param; const char *bootfile; IPaddr_t ip; char *file; char ip4_str[sizeof("255.255.255.255")]; struct eth_device *edev; struct dhcp_result *dhcp_res; am33xx_register_ethaddr(0, 0); memset(&dhcp_param, 0, sizeof(struct dhcp_req_param)); dhcp_param.vendor_id = "am335x barebox-mlo"; edev = eth_get_byname("eth0"); if (!edev) { printf("eth0 not found\n"); return NULL; } err = dhcp_request(edev, &dhcp_param, &dhcp_res); if (err) { printf("dhcp failed\n"); return NULL; } dhcp_set_result(edev, dhcp_res); edev->ifup = true; /* * Older tftp server don't send the file size. * Then tftpfs needs temporary place to store the file. */ err = mount("none", "ramfs", "/", NULL); if (err < 0) { printf("failed to mount ramfs\n"); return NULL; } err = make_directory(TFTP_MOUNT); if (err) return NULL; ip = net_get_serverip(); sprintf(ip4_str, "%pI4", &ip); err = mount(ip4_str, "tftp", TFTP_MOUNT, NULL); if (err < 0) { printf("Unable to mount.\n"); return NULL; } bootfile = dhcp_res->bootfile; if (!bootfile) { printf("bootfile not found.\n"); return NULL; } file = basprintf("%s/%s", TFTP_MOUNT, bootfile); buf = read_file(file, &len); if (!buf) printf("could not read %s.\n", bootfile); free(file); umount(TFTP_MOUNT); return buf; }
static int do_tftpb(int argc, char *argv[]) { char *source, *dest, *freep; int opt; unsigned long flags; int tftp_push = 0; int ret; IPaddr_t ip; while ((opt = getopt(argc, argv, "p")) > 0) { switch(opt) { case 'p': tftp_push = 1; break; default: return COMMAND_ERROR_USAGE; } } if (argc <= optind) return COMMAND_ERROR_USAGE; source = argv[optind++]; if (argc == optind) dest = basename(source); else dest = argv[optind]; if (tftp_push) { dest = freep = asprintf("%s/%s", TFTP_MOUNT_PATH, dest); flags = O_RDONLY; } else { source = freep = asprintf("%s/%s", TFTP_MOUNT_PATH, source); flags = O_WRONLY | O_CREAT; } if (!freep) return -ENOMEM; ret = make_directory(TFTP_MOUNT_PATH); if (ret) goto err_free; ip = net_get_serverip(); ret = mount(ip_to_string(ip), "tftp", TFTP_MOUNT_PATH); if (ret) goto err_rmdir; debug("%s: %s -> %s\n", __func__, source, dest); ret = copy_file(source, dest, 1); umount(TFTP_MOUNT_PATH); err_rmdir: rmdir(TFTP_MOUNT_PATH); err_free: free(freep); return ret; }