xref: /DragonOS/kernel/src/driver/video/fbdev/vesafb.rs (revision 401699735b5ec29768c3c0c47df6c529991f108f)
1 use core::{
2     ffi::{c_uint, c_void},
3     mem::MaybeUninit,
4     sync::atomic::AtomicBool,
5 };
6 
7 use alloc::{
8     string::{String, ToString},
9     sync::{Arc, Weak},
10     vec::Vec,
11 };
12 use system_error::SystemError;
13 use unified_init::macros::unified_init;
14 
15 use crate::{
16     arch::MMArch,
17     driver::{
18         base::{
19             class::Class,
20             device::{
21                 bus::Bus, device_manager, driver::Driver, Device, DeviceState, DeviceType, IdTable,
22             },
23             kobject::{KObjType, KObject, KObjectState, LockedKObjectState},
24             kset::KSet,
25             platform::{
26                 platform_device::{platform_device_manager, PlatformDevice},
27                 platform_driver::{platform_driver_manager, PlatformDriver},
28                 CompatibleTable,
29             },
30         },
31         serial::serial8250::send_to_default_serial8250_port,
32         video::fbdev::base::{fbmem::frame_buffer_manager, FbVisual, FRAME_BUFFER_SET},
33     },
34     filesystem::{
35         kernfs::KernFSInode,
36         sysfs::{file::sysfs_emit_str, Attribute, AttributeGroup, SysFSOpsSupport},
37         vfs::syscall::ModeType,
38     },
39     include::bindings::bindings::{
40         multiboot2_get_Framebuffer_info, multiboot2_iter, multiboot_tag_framebuffer_info_t,
41         FRAME_BUFFER_MAPPING_OFFSET,
42     },
43     init::{boot_params, initcall::INITCALL_DEVICE},
44     libs::{
45         align::page_align_up,
46         once::Once,
47         rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard},
48         spinlock::SpinLock,
49     },
50     mm::{
51         allocator::page_frame::PageFrameCount, no_init::pseudo_map_phys, MemoryManagementArch,
52         PhysAddr, VirtAddr,
53     },
54 };
55 
56 use super::base::{
57     fbmem::FbDevice, BlankMode, BootTimeVideoType, FbAccel, FbActivateFlags, FbId, FbState, FbType,
58     FbVModeFlags, FbVarScreenInfo, FbVideoMode, FixedScreenInfo, FrameBuffer, FrameBufferInfo,
59     FrameBufferInfoData, FrameBufferOps,
60 };
61 
62 /// 当前机器上面是否有vesa帧缓冲区
63 static HAS_VESA_FB: AtomicBool = AtomicBool::new(false);
64 
65 lazy_static! {
66     static ref VESAFB_FIX_INFO: RwLock<FixedScreenInfo> = RwLock::new(FixedScreenInfo {
67         id: FixedScreenInfo::name2id("VESA VGA"),
68         fb_type: FbType::PackedPixels,
69         accel: FbAccel::None,
70         ..Default::default()
71     });
72     static ref VESAFB_DEFINED: RwLock<FbVarScreenInfo> = RwLock::new(FbVarScreenInfo {
73         activate: FbActivateFlags::FB_ACTIVATE_NOW,
74         height: None,
75         width: None,
76         right_margin: 32,
77         upper_margin: 16,
78         lower_margin: 4,
79         vsync_len: 4,
80         vmode: FbVModeFlags::FB_VMODE_NONINTERLACED,
81 
82         ..Default::default()
83     });
84 }
85 
86 #[derive(Debug)]
87 #[cast_to([sync] Device)]
88 #[cast_to([sync] PlatformDevice)]
89 pub struct VesaFb {
90     inner: SpinLock<InnerVesaFb>,
91     kobj_state: LockedKObjectState,
92     fb_data: RwLock<FrameBufferInfoData>,
93 }
94 
95 impl VesaFb {
96     pub const NAME: &'static str = "vesa_vga";
97     pub fn new() -> Self {
98         let mut fb_info_data = FrameBufferInfoData::new();
99         fb_info_data.pesudo_palette.resize(256, 0);
100         return Self {
101             inner: SpinLock::new(InnerVesaFb {
102                 bus: None,
103                 class: None,
104                 driver: None,
105                 kern_inode: None,
106                 parent: None,
107                 kset: None,
108                 kobj_type: None,
109                 device_state: DeviceState::NotInitialized,
110                 pdev_id: 0,
111                 pdev_id_auto: false,
112                 fb_id: FbId::INIT,
113                 fb_device: None,
114                 fb_state: FbState::Suspended,
115             }),
116             kobj_state: LockedKObjectState::new(None),
117             fb_data: RwLock::new(fb_info_data),
118         };
119     }
120 }
121 
122 #[derive(Debug)]
123 struct InnerVesaFb {
124     bus: Option<Weak<dyn Bus>>,
125     class: Option<Arc<dyn Class>>,
126     driver: Option<Weak<dyn Driver>>,
127     kern_inode: Option<Arc<KernFSInode>>,
128     parent: Option<Weak<dyn KObject>>,
129     kset: Option<Arc<KSet>>,
130     kobj_type: Option<&'static dyn KObjType>,
131     device_state: DeviceState,
132     pdev_id: i32,
133     pdev_id_auto: bool,
134     fb_id: FbId,
135     fb_device: Option<Arc<FbDevice>>,
136     fb_state: FbState,
137 }
138 
139 impl FrameBuffer for VesaFb {
140     fn fb_id(&self) -> FbId {
141         self.inner.lock().fb_id
142     }
143 
144     fn set_fb_id(&self, id: FbId) {
145         self.inner.lock().fb_id = id;
146     }
147 }
148 
149 impl PlatformDevice for VesaFb {
150     fn pdev_name(&self) -> &str {
151         Self::NAME
152     }
153 
154     fn set_pdev_id(&self, id: i32) {
155         self.inner.lock().pdev_id = id;
156     }
157 
158     fn set_pdev_id_auto(&self, id_auto: bool) {
159         self.inner.lock().pdev_id_auto = id_auto;
160     }
161 
162     fn compatible_table(&self) -> CompatibleTable {
163         todo!()
164     }
165 
166     fn is_initialized(&self) -> bool {
167         self.inner.lock().device_state == DeviceState::Initialized
168     }
169 
170     fn set_state(&self, set_state: DeviceState) {
171         self.inner.lock().device_state = set_state;
172     }
173 }
174 
175 impl Device for VesaFb {
176     fn dev_type(&self) -> DeviceType {
177         DeviceType::Char
178     }
179 
180     fn id_table(&self) -> IdTable {
181         IdTable::new(self.name(), None)
182     }
183 
184     fn bus(&self) -> Option<Weak<dyn Bus>> {
185         self.inner.lock().bus.clone()
186     }
187 
188     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
189         self.inner.lock().bus = bus;
190     }
191 
192     fn set_class(&self, class: Option<Arc<dyn Class>>) {
193         self.inner.lock().class = class;
194     }
195 
196     fn driver(&self) -> Option<Arc<dyn Driver>> {
197         self.inner.lock().driver.clone()?.upgrade()
198     }
199 
200     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
201         self.inner.lock().driver = driver;
202     }
203 
204     fn is_dead(&self) -> bool {
205         false
206     }
207 
208     fn can_match(&self) -> bool {
209         true
210     }
211 
212     fn set_can_match(&self, _can_match: bool) {}
213 
214     fn state_synced(&self) -> bool {
215         true
216     }
217 }
218 
219 impl KObject for VesaFb {
220     fn as_any_ref(&self) -> &dyn core::any::Any {
221         self
222     }
223 
224     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
225         self.inner.lock().kern_inode = inode;
226     }
227 
228     fn inode(&self) -> Option<Arc<KernFSInode>> {
229         self.inner.lock().kern_inode.clone()
230     }
231 
232     fn parent(&self) -> Option<Weak<dyn KObject>> {
233         self.inner.lock().parent.clone()
234     }
235 
236     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
237         self.inner.lock().parent = parent;
238     }
239 
240     fn kset(&self) -> Option<Arc<KSet>> {
241         self.inner.lock().kset.clone()
242     }
243 
244     fn set_kset(&self, kset: Option<Arc<KSet>>) {
245         self.inner.lock().kset = kset;
246     }
247 
248     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
249         self.inner.lock().kobj_type
250     }
251 
252     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
253         self.inner.lock().kobj_type = ktype;
254     }
255 
256     fn name(&self) -> String {
257         Self::NAME.to_string()
258     }
259 
260     fn set_name(&self, _name: String) {
261         // do nothing
262     }
263 
264     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
265         self.kobj_state.read()
266     }
267 
268     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
269         self.kobj_state.write()
270     }
271 
272     fn set_kobj_state(&self, state: KObjectState) {
273         *self.kobj_state.write() = state;
274     }
275 }
276 
277 impl FrameBufferOps for VesaFb {
278     fn fb_open(&self, _user: bool) {
279         todo!()
280     }
281 
282     fn fb_release(&self, _user: bool) {
283         todo!()
284     }
285 
286     fn fb_set_color_register(
287         &self,
288         regno: u16,
289         mut red: u16,
290         mut green: u16,
291         mut blue: u16,
292     ) -> Result<(), SystemError> {
293         let mut fb_data = self.framebuffer_info_data().write();
294         let var = self.current_fb_var();
295         if regno as usize >= fb_data.pesudo_palette.len() {
296             return Err(SystemError::E2BIG);
297         }
298 
299         if var.bits_per_pixel == 8 {
300             todo!("vesa_setpalette todo");
301         } else if regno < 16 {
302             match var.bits_per_pixel {
303                 16 => {
304                     if var.red.offset == 10 {
305                         // RGB 1:5:5:5
306                         fb_data.pesudo_palette[regno as usize] = ((red as u32 & 0xf800) >> 1)
307                             | ((green as u32 & 0xf800) >> 6)
308                             | ((blue as u32 & 0xf800) >> 11);
309                     } else {
310                         fb_data.pesudo_palette[regno as usize] = (red as u32 & 0xf800)
311                             | ((green as u32 & 0xfc00) >> 5)
312                             | ((blue as u32 & 0xf800) >> 11);
313                     }
314                 }
315                 24 | 32 => {
316                     red >>= 8;
317                     green >>= 8;
318                     blue >>= 8;
319                     fb_data.pesudo_palette[regno as usize] = ((red as u32) << var.red.offset)
320                         | ((green as u32) << var.green.offset)
321                         | ((blue as u32) << var.blue.offset);
322                 }
323                 _ => {}
324             }
325         }
326 
327         Ok(())
328     }
329 
330     fn fb_blank(&self, _blank_mode: BlankMode) -> Result<(), SystemError> {
331         todo!()
332     }
333 
334     fn fb_destroy(&self) {
335         todo!()
336     }
337 
338     fn fb_read(&self, buf: &mut [u8], pos: usize) -> Result<usize, SystemError> {
339         let bp = boot_params().read();
340 
341         let vaddr = bp.screen_info.lfb_virt_base.ok_or(SystemError::ENODEV)?;
342         let size = self.current_fb_fix().smem_len;
343         drop(bp);
344         if pos >= size {
345             return Ok(0);
346         }
347 
348         let pos = pos as i64;
349         let size = size as i64;
350 
351         let len = core::cmp::min(size - pos, buf.len() as i64) as usize;
352 
353         let slice = unsafe { core::slice::from_raw_parts(vaddr.as_ptr::<u8>(), size as usize) };
354         buf[..len].copy_from_slice(&slice[pos as usize..(pos as usize + len)]);
355 
356         return Ok(len);
357     }
358 
359     fn fb_write(&self, buf: &[u8], pos: usize) -> Result<usize, SystemError> {
360         let bp = boot_params().read();
361 
362         let vaddr = bp.screen_info.lfb_virt_base.ok_or(SystemError::ENODEV)?;
363         let size = self.current_fb_fix().smem_len;
364 
365         if pos >= size {
366             return Ok(0);
367         }
368 
369         let pos = pos as i64;
370         let size = size as i64;
371 
372         let len = core::cmp::min(size - pos, buf.len() as i64) as usize;
373 
374         let slice = unsafe { core::slice::from_raw_parts_mut(vaddr.as_ptr::<u8>(), size as usize) };
375         slice[pos as usize..(pos as usize + len)].copy_from_slice(&buf[..len]);
376 
377         return Ok(len);
378     }
379 
380     fn fb_image_blit(&self, image: &super::base::FbImage) {
381         self.generic_imageblit(image);
382     }
383 
384     /// ## 填充矩形
385     fn fb_fillrect(&self, rect: super::base::FillRectData) -> Result<(), SystemError> {
386         // kwarn!("rect {rect:?}");
387 
388         let boot_param = boot_params().read();
389         let screen_base = boot_param
390             .screen_info
391             .lfb_virt_base
392             .ok_or(SystemError::ENODEV)?;
393 
394         let fg = if self.current_fb_fix().visual == FbVisual::TrueColor
395             || self.current_fb_fix().visual == FbVisual::DirectColor
396         {
397             self.fb_data.read().pesudo_palette[rect.color as usize]
398         } else {
399             rect.color
400         };
401 
402         let bpp = self.current_fb_var().bits_per_pixel;
403         // 每行像素数
404         let line_offset = self.current_fb_var().xres;
405         match bpp {
406             32 => {
407                 let base = screen_base.as_ptr::<u32>();
408 
409                 for y in rect.dy..(rect.dy + rect.height) {
410                     for x in rect.dx..(rect.dx + rect.width) {
411                         unsafe { *base.add((y * line_offset + x) as usize) = fg };
412                     }
413                 }
414             }
415             _ => todo!(),
416         }
417 
418         Ok(())
419     }
420 
421     #[inline(never)]
422     fn fb_copyarea(&self, data: super::base::CopyAreaData) {
423         let bp = boot_params().read();
424         let base = bp.screen_info.lfb_virt_base.unwrap();
425         let var = self.current_fb_var();
426 
427         // 原区域或者目标区域全在屏幕外,则直接返回
428         if data.sx > var.xres as i32
429             || data.sy > var.yres as i32
430             || data.dx > var.xres as i32
431             || data.dy > var.yres as i32
432             || (data.sx + data.width as i32) < 0
433             || (data.sy + data.height as i32) < 0
434             || (data.dx + data.width as i32) < 0
435             || (data.dy + data.height as i32) < 0
436         {
437             return;
438         }
439 
440         // 求两个矩形可视范围交集
441         let (s_visiable_x, s_w) = if data.sx < 0 {
442             (0, (data.width - ((-data.sx) as u32)).min(var.xres))
443         } else {
444             let w = if data.sx as u32 + data.width > var.xres {
445                 var.xres - data.sx as u32
446             } else {
447                 data.width
448             };
449 
450             (data.sx, w)
451         };
452         let (s_visiable_y, s_h) = if data.sy < 0 {
453             (0, (data.height - ((-data.sy) as u32).min(var.yres)))
454         } else {
455             let h = if data.sy as u32 + data.height > var.yres {
456                 var.yres - data.sy as u32
457             } else {
458                 data.height
459             };
460 
461             (data.sy, h)
462         };
463 
464         let (d_visiable_x, d_w) = if data.dx < 0 {
465             (0, (data.width - ((-data.dx) as u32)).min(var.xres))
466         } else {
467             let w = if data.dx as u32 + data.width > var.xres {
468                 var.xres - data.dx as u32
469             } else {
470                 data.width
471             };
472 
473             (data.dx, w)
474         };
475         let (d_visiable_y, d_h) = if data.dy < 0 {
476             (0, (data.height - ((-data.dy) as u32).min(var.yres)))
477         } else {
478             let h = if data.dy as u32 + data.height > var.yres {
479                 var.yres - data.dy as u32
480             } else {
481                 data.height
482             };
483 
484             (data.dy, h)
485         };
486 
487         // 可视范围无交集
488         if !(d_h + s_h > data.height && s_w + d_w > data.width) {
489             return;
490         }
491 
492         // 可视区域左上角相对于矩形的坐标
493         let s_relative_x = s_visiable_x - data.sx;
494         let s_relative_y = s_visiable_y - data.sy;
495         let d_relative_x = d_visiable_x - data.dx;
496         let d_relative_y = d_visiable_y - data.dy;
497 
498         let visiable_x = s_relative_x.max(d_relative_x);
499         let visiable_y = s_relative_y.max(d_relative_y);
500         let visiable_h = d_h + s_h - data.height;
501         let visiable_w = d_w + s_w - data.width;
502 
503         let s_real_x = (visiable_x + data.sx) as u32;
504         let s_real_y = (visiable_y + data.sy) as u32;
505         let d_real_x = (visiable_x + data.dx) as u32;
506         let d_real_y = (visiable_y + data.dy) as u32;
507 
508         let bytes_per_pixel = var.bits_per_pixel >> 3;
509         let bytes_per_line = var.xres * bytes_per_pixel;
510 
511         let src =
512             base + VirtAddr::new((s_real_y * bytes_per_line + s_real_x * bytes_per_pixel) as usize);
513 
514         let dst =
515             base + VirtAddr::new((d_real_y * bytes_per_line + d_real_x * bytes_per_pixel) as usize);
516 
517         let size = (visiable_h * visiable_w) as usize;
518 
519         match bytes_per_pixel {
520             4 => {
521                 // 32bpp
522                 let mut dst = dst.as_ptr::<u32>();
523                 let mut src = src.as_ptr::<u32>();
524                 let line_offset = var.xres as usize;
525 
526                 if s_real_x > d_real_x {
527                     // 如果src在dst下方,则可以直接拷贝不会出现指针覆盖
528                     unsafe {
529                         for _ in 0..visiable_h {
530                             core::ptr::copy(src, dst, visiable_w as usize);
531                             src = src.add(line_offset);
532                             dst = dst.add(visiable_w as usize);
533                         }
534                     }
535                 } else {
536                     let mut tmp: Vec<u32> = vec![0; size];
537                     let mut tmp_ptr = tmp.as_mut_ptr();
538 
539                     // 这里是一个可以优化的点,现在为了避免指针拷贝时覆盖,统一先拷贝进入buf再拷贝到dst
540                     unsafe {
541                         for _ in 0..visiable_h {
542                             core::ptr::copy(src, tmp_ptr, visiable_w as usize);
543                             src = src.add(line_offset);
544                             tmp_ptr = tmp_ptr.add(visiable_w as usize);
545                         }
546 
547                         tmp_ptr = tmp_ptr.sub(size);
548                         for _ in 0..visiable_h {
549                             core::ptr::copy(tmp_ptr, dst, visiable_w as usize);
550                             dst = dst.add(line_offset);
551                             tmp_ptr = tmp_ptr.add(visiable_w as usize);
552                         }
553                     }
554                 }
555             }
556             _ => {
557                 todo!()
558             }
559         }
560     }
561 }
562 
563 impl FrameBufferInfo for VesaFb {
564     fn fb_device(&self) -> Option<Arc<FbDevice>> {
565         self.inner.lock().fb_device.clone()
566     }
567 
568     fn set_fb_device(&self, device: Option<Arc<FbDevice>>) {
569         self.inner.lock().fb_device = device;
570     }
571 
572     fn screen_size(&self) -> usize {
573         todo!()
574     }
575 
576     fn current_fb_var(&self) -> FbVarScreenInfo {
577         *VESAFB_DEFINED.read()
578     }
579 
580     fn current_fb_fix(&self) -> FixedScreenInfo {
581         *VESAFB_FIX_INFO.read()
582     }
583 
584     fn video_mode(&self) -> Option<&FbVideoMode> {
585         todo!()
586     }
587 
588     fn state(&self) -> FbState {
589         self.inner.lock().fb_state
590     }
591 
592     fn framebuffer_info_data(&self) -> &RwLock<FrameBufferInfoData> {
593         &self.fb_data
594     }
595 }
596 
597 #[derive(Debug)]
598 #[cast_to([sync] PlatformDriver)]
599 struct VesaFbDriver {
600     inner: SpinLock<InnerVesaFbDriver>,
601     kobj_state: LockedKObjectState,
602 }
603 
604 impl VesaFbDriver {
605     pub fn new() -> Arc<Self> {
606         let r = Arc::new(Self {
607             inner: SpinLock::new(InnerVesaFbDriver {
608                 ktype: None,
609                 kset: None,
610                 parent: None,
611                 kernfs_inode: None,
612                 devices: Vec::new(),
613                 bus: None,
614                 self_ref: Weak::new(),
615             }),
616             kobj_state: LockedKObjectState::new(None),
617         });
618 
619         r.inner.lock().self_ref = Arc::downgrade(&r);
620 
621         return r;
622     }
623 }
624 
625 #[derive(Debug)]
626 struct InnerVesaFbDriver {
627     ktype: Option<&'static dyn KObjType>,
628     kset: Option<Arc<KSet>>,
629     parent: Option<Weak<dyn KObject>>,
630     kernfs_inode: Option<Arc<KernFSInode>>,
631     devices: Vec<Arc<dyn Device>>,
632     bus: Option<Weak<dyn Bus>>,
633 
634     self_ref: Weak<VesaFbDriver>,
635 }
636 
637 impl VesaFbDriver {
638     const NAME: &'static str = "vesa-framebuffer";
639 }
640 
641 impl PlatformDriver for VesaFbDriver {
642     fn probe(&self, device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
643         let device = device
644             .clone()
645             .arc_any()
646             .downcast::<VesaFb>()
647             .map_err(|_| SystemError::EINVAL)?;
648 
649         device.set_driver(Some(self.inner.lock_irqsave().self_ref.clone()));
650 
651         return Ok(());
652     }
653 
654     fn remove(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
655         todo!()
656     }
657 
658     fn shutdown(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
659         // do nothing
660         return Ok(());
661     }
662 
663     fn suspend(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
664         // do nothing
665         return Ok(());
666     }
667 
668     fn resume(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
669         todo!()
670     }
671 }
672 
673 impl Driver for VesaFbDriver {
674     fn id_table(&self) -> Option<IdTable> {
675         Some(IdTable::new(VesaFb::NAME.to_string(), None))
676     }
677 
678     fn devices(&self) -> Vec<Arc<dyn Device>> {
679         self.inner.lock().devices.clone()
680     }
681 
682     fn add_device(&self, device: Arc<dyn Device>) {
683         let mut guard = self.inner.lock();
684         // check if the device is already in the list
685         if guard.devices.iter().any(|dev| Arc::ptr_eq(dev, &device)) {
686             return;
687         }
688 
689         guard.devices.push(device);
690     }
691 
692     fn delete_device(&self, device: &Arc<dyn Device>) {
693         let mut guard = self.inner.lock();
694         guard.devices.retain(|dev| !Arc::ptr_eq(dev, device));
695     }
696 
697     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
698         self.inner.lock().bus = bus;
699     }
700 
701     fn bus(&self) -> Option<Weak<dyn Bus>> {
702         self.inner.lock().bus.clone()
703     }
704 
705     fn dev_groups(&self) -> &'static [&'static dyn AttributeGroup] {
706         return &[&VesaFbAnonAttributeGroup];
707     }
708 }
709 
710 impl KObject for VesaFbDriver {
711     fn as_any_ref(&self) -> &dyn core::any::Any {
712         self
713     }
714 
715     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
716         self.inner.lock().kernfs_inode = inode;
717     }
718 
719     fn inode(&self) -> Option<Arc<KernFSInode>> {
720         self.inner.lock().kernfs_inode.clone()
721     }
722 
723     fn parent(&self) -> Option<Weak<dyn KObject>> {
724         self.inner.lock().parent.clone()
725     }
726 
727     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
728         self.inner.lock().parent = parent;
729     }
730 
731     fn kset(&self) -> Option<Arc<KSet>> {
732         self.inner.lock().kset.clone()
733     }
734 
735     fn set_kset(&self, kset: Option<Arc<KSet>>) {
736         self.inner.lock().kset = kset;
737     }
738 
739     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
740         self.inner.lock().ktype
741     }
742 
743     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
744         self.inner.lock().ktype = ktype;
745     }
746 
747     fn name(&self) -> String {
748         Self::NAME.to_string()
749     }
750 
751     fn set_name(&self, _name: String) {
752         // do nothing
753     }
754 
755     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
756         self.kobj_state.read()
757     }
758 
759     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
760         self.kobj_state.write()
761     }
762 
763     fn set_kobj_state(&self, state: KObjectState) {
764         *self.kobj_state.write() = state;
765     }
766 }
767 
768 #[derive(Debug)]
769 struct VesaFbAnonAttributeGroup;
770 
771 impl AttributeGroup for VesaFbAnonAttributeGroup {
772     fn name(&self) -> Option<&str> {
773         None
774     }
775 
776     fn attrs(&self) -> &[&'static dyn Attribute] {
777         &[&AnonAttrPhysAddr as &'static dyn Attribute]
778     }
779 
780     fn is_visible(
781         &self,
782         _kobj: Arc<dyn KObject>,
783         attr: &'static dyn Attribute,
784     ) -> Option<ModeType> {
785         Some(attr.mode())
786     }
787 }
788 
789 #[derive(Debug)]
790 struct AnonAttrPhysAddr;
791 
792 impl Attribute for AnonAttrPhysAddr {
793     fn name(&self) -> &str {
794         "smem_start"
795     }
796 
797     fn mode(&self) -> ModeType {
798         ModeType::S_IRUGO
799     }
800 
801     fn support(&self) -> SysFSOpsSupport {
802         SysFSOpsSupport::ATTR_SHOW
803     }
804 
805     fn show(&self, _kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> {
806         sysfs_emit_str(
807             buf,
808             format!(
809                 "0x{:x}\n",
810                 VESAFB_FIX_INFO
811                     .read()
812                     .smem_start
813                     .unwrap_or(PhysAddr::new(0))
814                     .data()
815             )
816             .as_str(),
817         )
818     }
819 }
820 
821 #[unified_init(INITCALL_DEVICE)]
822 pub fn vesa_fb_driver_init() -> Result<(), SystemError> {
823     let driver = VesaFbDriver::new();
824 
825     platform_driver_manager().register(driver)?;
826 
827     return Ok(());
828 }
829 
830 /// 在内存管理初始化之前,初始化vesafb
831 pub fn vesafb_early_init() -> Result<VirtAddr, SystemError> {
832     let mut _reserved: u32 = 0;
833 
834     let mut fb_info: MaybeUninit<multiboot_tag_framebuffer_info_t> = MaybeUninit::uninit();
835     //从multiboot2中读取帧缓冲区信息至fb_info
836 
837     // todo: 换成rust的,并且检测是否成功获取
838     unsafe {
839         multiboot2_iter(
840             Some(multiboot2_get_Framebuffer_info),
841             fb_info.as_mut_ptr() as usize as *mut c_void,
842             &mut _reserved as *mut c_uint,
843         )
844     };
845     unsafe { fb_info.assume_init() };
846     let fb_info: multiboot_tag_framebuffer_info_t = unsafe { core::mem::transmute(fb_info) };
847 
848     // todo: 判断是否有vesa帧缓冲区,这里暂时直接设置true
849     HAS_VESA_FB.store(true, core::sync::atomic::Ordering::SeqCst);
850 
851     let width = fb_info.framebuffer_width;
852     let height = fb_info.framebuffer_height;
853 
854     let mut boot_params_guard = boot_params().write();
855     let boottime_screen_info = &mut boot_params_guard.screen_info;
856 
857     boottime_screen_info.is_vga = true;
858 
859     boottime_screen_info.lfb_base = PhysAddr::new(fb_info.framebuffer_addr as usize);
860 
861     if fb_info.framebuffer_type == 2 {
862         //当type=2时,width与height用字符数表示,故depth=8
863         boottime_screen_info.origin_video_cols = width as u8;
864         boottime_screen_info.origin_video_lines = height as u8;
865         boottime_screen_info.video_type = BootTimeVideoType::Mda;
866         boottime_screen_info.lfb_depth = 8;
867     } else {
868         //否则为图像模式,depth应参照帧缓冲区信息里面的每个像素的位数
869         boottime_screen_info.lfb_width = width;
870         boottime_screen_info.lfb_height = height;
871         boottime_screen_info.video_type = BootTimeVideoType::Vlfb;
872         boottime_screen_info.lfb_depth = fb_info.framebuffer_bpp as u8;
873     }
874 
875     boottime_screen_info.lfb_size =
876         (width * height * ((fb_info.framebuffer_bpp as u32 + 7) / 8)) as usize;
877 
878     // let buf_vaddr = VirtAddr::new(0xffff800003200000);
879     let buf_vaddr = VirtAddr::new(
880         crate::include::bindings::bindings::SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE as usize
881             + FRAME_BUFFER_MAPPING_OFFSET as usize,
882     );
883     boottime_screen_info.lfb_virt_base = Some(buf_vaddr);
884 
885     let init_text = "Video driver to map.\n\0";
886     send_to_default_serial8250_port(init_text.as_bytes());
887 
888     // 地址映射
889     let paddr = PhysAddr::new(fb_info.framebuffer_addr as usize);
890     let count =
891         PageFrameCount::new(page_align_up(boottime_screen_info.lfb_size) / MMArch::PAGE_SIZE);
892     unsafe { pseudo_map_phys(buf_vaddr, paddr, count) };
893     return Ok(buf_vaddr);
894 }
895 
896 #[unified_init(INITCALL_DEVICE)]
897 fn vesa_fb_device_init() -> Result<(), SystemError> {
898     // 如果没有vesa帧缓冲区,直接返回
899     if !HAS_VESA_FB.load(core::sync::atomic::Ordering::SeqCst) {
900         return Ok(());
901     }
902 
903     static INIT: Once = Once::new();
904     INIT.call_once(|| {
905         kinfo!("vesa fb device init");
906         let device = Arc::new(VesaFb::new());
907 
908         let mut fb_fix = VESAFB_FIX_INFO.write_irqsave();
909         let mut fb_var = VESAFB_DEFINED.write_irqsave();
910 
911         let boot_params_guard = boot_params().read();
912         let boottime_screen_info = &boot_params_guard.screen_info;
913 
914         fb_fix.smem_start = Some(boottime_screen_info.lfb_base);
915         fb_fix.smem_len = boottime_screen_info.lfb_size;
916 
917         if boottime_screen_info.video_type == BootTimeVideoType::Mda {
918             fb_fix.visual = FbVisual::Mono10;
919             fb_var.bits_per_pixel = 8;
920             fb_fix.line_length =
921                 (boottime_screen_info.origin_video_cols as u32) * (fb_var.bits_per_pixel / 8);
922             fb_var.xres_virtual = boottime_screen_info.origin_video_cols as u32;
923             fb_var.yres_virtual = boottime_screen_info.origin_video_lines as u32;
924         } else {
925             fb_fix.visual = FbVisual::TrueColor;
926             fb_var.bits_per_pixel = boottime_screen_info.lfb_depth as u32;
927             fb_fix.line_length =
928                 (boottime_screen_info.lfb_width as u32) * (fb_var.bits_per_pixel / 8);
929             fb_var.xres_virtual = boottime_screen_info.lfb_width as u32;
930             fb_var.yres_virtual = boottime_screen_info.lfb_height as u32;
931             fb_var.xres = boottime_screen_info.lfb_width as u32;
932             fb_var.yres = boottime_screen_info.lfb_height as u32;
933         }
934 
935         fb_var.red.length = boottime_screen_info.red_size as u32;
936         fb_var.green.length = boottime_screen_info.green_size as u32;
937         fb_var.blue.length = boottime_screen_info.blue_size as u32;
938 
939         fb_var.red.offset = boottime_screen_info.red_pos as u32;
940         fb_var.green.offset = boottime_screen_info.green_pos as u32;
941         fb_var.blue.offset = boottime_screen_info.blue_pos as u32;
942 
943         // TODO: 这里是暂时这样写的,初始化为RGB888格式,后续vesa初始化完善后删掉下面
944         fb_var.red.offset = 16;
945         fb_var.green.offset = 8;
946         fb_var.blue.offset = 0;
947 
948         if fb_var.bits_per_pixel >= 1 && fb_var.bits_per_pixel <= 8 {
949             fb_var.red.length = fb_var.bits_per_pixel;
950             fb_var.green.length = fb_var.bits_per_pixel;
951             fb_var.blue.length = fb_var.bits_per_pixel;
952         }
953 
954         device_manager().device_default_initialize(&(device.clone() as Arc<dyn Device>));
955 
956         platform_device_manager()
957             .device_add(device.clone() as Arc<dyn PlatformDevice>)
958             .expect("vesa_fb_device_init: platform_device_manager().device_add failed");
959 
960         frame_buffer_manager()
961             .register_fb(device.clone() as Arc<dyn FrameBuffer>)
962             .expect("vesa_fb_device_init: frame_buffer_manager().register_fb failed");
963 
964         // 加入全局fb表
965         let mut guard = FRAME_BUFFER_SET.write();
966         if guard.get(device.fb_id().data() as usize).unwrap().is_some() {
967             kwarn!(
968                 "vesa_fb_device_init: There is already an element {:?} in the FRAME_BUFFER_SET",
969                 device.fb_id()
970             );
971         }
972         guard[device.fb_id().data() as usize] = Some(device.clone());
973 
974         // 设置vesa fb的状态为运行中
975         device.inner.lock().fb_state = FbState::Running;
976     });
977 
978     return Ok(());
979 }
980