int disk_add(struct disk* d) { if (disk_count == MAX_DISKS) return -1; memcpy("disk\0\0", d->sysname, 7); utoa_s(disk_count, &d->sysname[4], 10); d->id = disk_count; /* Prepare device */ device_t* dev = device_create(0x8000 | d->id, d->sysname, DEVTYPE_BLOCK | DEVTYPE_SEEKABLE, NULL); d->dev = dev; device_set_description(dev, d->disklabel); d->b_size = (d->b_size == 0) ? 512 : d->b_size; dev->b_size = d->b_size; dev->__dev_read = &_disk_dev_read_wrapper; uint32_t diskmb = (uint32_t)(d->b_size * d->b_count / 1048576) & 0xffffffff; knotice("DISK: Adding disk %s (%d MB) as %s", d->disk_name, diskmb, d->sysname); kprintf("\n %s: %s (%d MB)", d->sysname, d->disk_name, diskmb); if (!disk_cmd_added) { kshell_add("disks", "List disks and their info", disk_cmd); disk_cmd_added = 1; } disks[disk_count] = *d; return disk_count++; }
static inline size_t snprint_pointer(char *buf, size_t size, void *ptr, bool left, char padding, size_t width) { char str[35] = "0x"; size_t len = utoa_s((unsigned int)ptr, 16, str + 2, sizeof(str) - 2); if (len == 0) return 0; len += 2; return copy_with_padding(buf, size, str, len, left, padding, width); }
static size_t itoa_s(int i, unsigned int base, char *buf, size_t len) { unsigned int v; if (base == 10 && i < 0) { if (len < 1) return 0; v = -i; len = utoa_s(v, base, buf + 1, len - 1); if (len == 0) return 0; *buf = '-'; return len + 1; } v = i; return utoa_s(v, base, buf, len); }
static inline size_t snprint_uint(char *buf, size_t size, unsigned int i, unsigned int base, bool left, char padding, size_t width) { char str[33]; size_t len = utoa_s(i, base, str, sizeof(str)); if (len == 0) return 0; return copy_with_padding(buf, size, str, len, left, padding, width); }