void sigchld() { int wstat; int pid; while ((pid = wait_nohang(&wstat)) > 0) { fprintf(stderr, "end: pid %d status %d\n", pid, wstat); if (autokill != 0) ptable_remove(pt, limit, pid); if (numchildren) --numchildren; printstatus(); } }
// return NULL for success, error string for failure int lkb_handle_command(lkb_t *lkb, const char *cmd, const char *arg, size_t len, const char **result) { *result = NULL; struct lkb_command *lcmd; for (lcmd = lkb_cmd_list; lcmd; lcmd = lcmd->next) { if (!strcmp(lcmd->name, cmd)) { *result = lcmd->handler(lkb, arg, len, lcmd->cookie); return 0; } } if (!strcmp(cmd, "flash") || !strcmp(cmd, "erase")) { struct ptable_entry entry; bdev_t *bdev; if (ptable_find(arg, &entry) < 0) { size_t plen = len; /* doesn't exist, make one */ if (ptable_add(arg, plen, 0) < 0) { *result = "error creating partition"; return -1; } if (ptable_find(arg, &entry) < 0) { *result = "couldn't find partition after creating it"; return -1; } } if (len > entry.length) { *result = "partition too small"; return -1; } if (!(bdev = ptable_get_device())) { *result = "ptable_get_device failed"; return -1; } printf("lkboot: erasing partition of size %llu\n", entry.length); if (bio_erase(bdev, entry.offset, entry.length) != (ssize_t)entry.length) { *result = "bio_erase failed"; return -1; } if (!strcmp(cmd, "flash")) { printf("lkboot: writing to partition\n"); void *buf = malloc(bdev->block_size); if (!buf) { *result = "memory allocation failed"; return -1; } size_t pos = 0; while (pos < len) { size_t toread = MIN(len - pos, bdev->block_size); LTRACEF("offset %zu, toread %zu\n", pos, toread); if (lkb_read(lkb, buf, toread)) { *result = "io error"; free(buf); return -1; } if (bio_write(bdev, buf, entry.offset + pos, toread) != (ssize_t)toread) { *result = "bio_write failed"; free(buf); return -1; } pos += toread; } free(buf); } } else if (!strcmp(cmd, "remove")) { if (ptable_remove(arg) < 0) { *result = "remove failed"; return -1; } } else if (!strcmp(cmd, "fpga")) { #if PLATFORM_ZYNQ void *buf = malloc(len); if (!buf) { *result = "error allocating buffer"; return -1; } /* translate to physical address */ paddr_t pa = vaddr_to_paddr(buf); if (pa == 0) { *result = "error allocating buffer"; free(buf); return -1; } if (lkb_read(lkb, buf, len)) { *result = "io error"; free(buf); return -1; } /* make sure the cache is flushed for this buffer for DMA coherency purposes */ arch_clean_cache_range((vaddr_t)buf, len); /* program the fpga */ zynq_reset_fpga(); zynq_program_fpga(pa, len); free(buf); #else *result = "no fpga"; return -1; #endif } else if (!strcmp(cmd, "boot")) { return do_boot(lkb, len, result); } else if (!strcmp(cmd, "getsysparam")) { const void *ptr; size_t len; if (sysparam_get_ptr(arg, &ptr, &len) == 0) { lkb_write(lkb, ptr, len); } } else if (!strcmp(cmd, "reboot")) { thread_resume(thread_create("reboot", &do_reboot, NULL, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE)); } else { *result = "unknown command"; return -1; } return 0; }
// return NULL for success, error string for failure const char *lkb_handle_command(lkb_t *lkb, const char *cmd, const char *arg, unsigned len) { struct lkb_command *lcmd; for (lcmd = lkb_cmd_list; lcmd; lcmd = lcmd->next) { if (!strcmp(lcmd->name, cmd)) { return lcmd->handler(lkb, arg, len, lcmd->cookie); } } if (len > lkb_iobuffer_size) { return "buffer too small"; } if (!strcmp(cmd, "flash") || !strcmp(cmd, "erase")) { struct ptable_entry entry; bdev_t *bdev; if (ptable_find(arg, &entry) < 0) { size_t plen = len; /* doesn't exist, make one */ #if PLATFORM_ZYNQ /* XXX not really the right place, should be in the ptable/bio layer */ plen = ROUNDUP(plen, 256*1024); #endif off_t off = ptable_allocate(plen, 0); if (off < 0) { return "no space to allocate partition"; } if (ptable_add(arg, off, plen, 0) < 0) { return "error creating partition"; } if (ptable_find(arg, &entry) < 0) { return "couldn't find partition after creating it"; } } if (len > entry.length) { return "partition too small"; } if (lkb_read(lkb, lkb_iobuffer, len)) { return "io error"; } if (!(bdev = bio_open(bootdevice))) { return "bio_open failed"; } if (bio_erase(bdev, entry.offset, entry.length) != (ssize_t)entry.length) { bio_close(bdev); return "bio_erase failed"; } if (!strcmp(cmd, "flash")) { if (bio_write(bdev, lkb_iobuffer, entry.offset, len) != (ssize_t)len) { bio_close(bdev); return "bio_write failed"; } } bio_close(bdev); return NULL; } else if (!strcmp(cmd, "remove")) { if (ptable_remove(arg) < 0) { return "remove failed"; } return NULL; } else if (!strcmp(cmd, "fpga")) { #if PLATFORM_ZYNQ unsigned *x = lkb_iobuffer; if (lkb_read(lkb, lkb_iobuffer, len)) { return "io error"; } for (unsigned n = 0; n < len; n+= 4) { *x = SWAP_32(*x); x++; } zynq_reset_fpga(); zynq_program_fpga(lkb_iobuffer_phys, len); return NULL; #else return "no fpga"; #endif } else if (!strcmp(cmd, "boot")) { if (lkb_read(lkb, lkb_iobuffer, len)) { return "io error"; } thread_resume(thread_create("boot", &do_boot, NULL, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE)); return NULL; } else if (!strcmp(cmd, "getsysparam")) { const void *ptr; size_t len; if (sysparam_get_ptr(arg, &ptr, &len) == 0) { lkb_write(lkb, ptr, len); } return NULL; } else if (!strcmp(cmd, "reboot")) { thread_resume(thread_create("reboot", &do_reboot, NULL, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE)); return NULL; } else { return "unknown command"; } }